Skip to content

Commit

Permalink
initial import
Browse files Browse the repository at this point in the history
  • Loading branch information
pcarion committed Dec 7, 2015
1 parent 41253e7 commit 5f9eaa5
Show file tree
Hide file tree
Showing 7 changed files with 205 additions and 0 deletions.
30 changes: 30 additions & 0 deletions .eslintrc
@@ -0,0 +1,30 @@
{
"parser": "babel-eslint",
"rules": {
"quotes": [
2,
"single"
],
"linebreak-style": [
2,
"unix"
],
"no-unused-vars": [2, {
"vars": "all",
"args": "after-used",
"argsIgnorePattern": "^_",
"varsIgnorePattern": "^_"
}],
"semi": [
2,
"always"
],
"no-console" : 0
},
"env": {
"es6": true,
"node": true
},
"plugins": [
]
}
2 changes: 2 additions & 0 deletions .gitignore
Expand Up @@ -25,3 +25,5 @@ build/Release
# Dependency directory
# https://www.npmjs.org/doc/misc/npm-faq.html#should-i-check-my-node_modules-folder-into-git
node_modules
client_secret.json
gmail-credentials.json
47 changes: 47 additions & 0 deletions lib/get_token.js
@@ -0,0 +1,47 @@
(function() {
'use strict';

var fs = require('fs');
var googleAuth = require('google-auth-library');

function getAuthorizationToken(code, cb) {
// Load client secrets
fs.readFile('client_secret.json', function(err, data) {
if (err) {
return cb(err);
}
var credentials = JSON.parse(data);
var clientSecret = credentials.installed.client_secret;
var clientId = credentials.installed.client_id;
var redirectUrl = credentials.installed.redirect_uris[0];
var auth = new googleAuth();
var oauth2Client = new auth.OAuth2(clientId, clientSecret, redirectUrl);

oauth2Client.getToken(code, function(err, token) {
if (err) {
return cb(err);
}
var file = 'gmail-credentials.json';

fs.writeFile(file, JSON.stringify(token));
return cb(null, file);
});
});
}

if (process.argv.length != 3) {
console.log('usage: node get_token token');
process.exit(1);
}
var token = process.argv[2];

getAuthorizationToken(token, function(err, file) {
if (err) {
console.log('err:', err);
} else {
console.log('authorization token is in:\n', file);
}
});

})();

39 changes: 39 additions & 0 deletions lib/get_url.js
@@ -0,0 +1,39 @@
(function() {
'use strict';

var fs = require('fs');
var googleAuth = require('google-auth-library');

var scopes = require('./scopes');

function getAuthorizationUrl(cb) {
// Load client secrets
fs.readFile('client_secret.json', function(err, data) {
if (err) {
return cb(err);
}
var credentials = JSON.parse(data);
var clientSecret = credentials.installed.client_secret;
var clientId = credentials.installed.client_id;
var redirectUrl = credentials.installed.redirect_uris[0];
var auth = new googleAuth();
var oauth2Client = new auth.OAuth2(clientId, clientSecret, redirectUrl);

var authUrl = oauth2Client.generateAuthUrl({
access_type: 'offline',
scope: scopes
});
return cb(null, authUrl);
});
}

getAuthorizationUrl(function(err, url) {
if (err) {
console.log('err:', err);
} else {
console.log('Authorization url is:\n', url);
}
});

})();

7 changes: 7 additions & 0 deletions lib/scopes.js
@@ -0,0 +1,7 @@
(function() {
"use strict";

module.exports = [
'https://www.googleapis.com/auth/gmail.send'
];
})();
76 changes: 76 additions & 0 deletions lib/send_mail.js
@@ -0,0 +1,76 @@
(function() {
'use strict';

var fs = require('fs');
var googleAuth = require('google-auth-library');
var google = require('googleapis');

function getOAuth2Client(cb) {
// Load client secrets
fs.readFile('client_secret.json', function(err, data) {
if (err) {
return cb(err);
}
var credentials = JSON.parse(data);
var clientSecret = credentials.installed.client_secret;
var clientId = credentials.installed.client_id;
var redirectUrl = credentials.installed.redirect_uris[0];
var auth = new googleAuth();
var oauth2Client = new auth.OAuth2(clientId, clientSecret, redirectUrl);

// Load credentials
fs.readFile('gmail-credentials.json', function(err, token) {
if (err) {
return cb(err);
} else {
oauth2Client.credentials = JSON.parse(token);
return cb(null, oauth2Client);
}
});
});
}

function sendSampleMail(auth, cb) {
var gmailClass = google.gmail('v1');

var email_lines = [];

email_lines.push('From: "test" <pcarion@gmail.com>');
email_lines.push('To: pcarion@gmail.com');
email_lines.push('Content-type: text/html;charset=iso-8859-1');
email_lines.push('MIME-Version: 1.0');
email_lines.push('Subject: this would be the subject');
email_lines.push('');
email_lines.push('And this would be the content.<br/>');
email_lines.push('The body is in HTML so <b>we could even use bold</b>');

var email = email_lines.join('\r\n').trim();

var base64EncodedEmail = new Buffer(email).toString('base64');
base64EncodedEmail = base64EncodedEmail.replace(/\+/g, '-').replace(/\//g, '_');

gmailClass.users.messages.send({
auth: auth,
userId: 'me',
resource: {
raw: base64EncodedEmail
}
}, cb);
}


getOAuth2Client(function(err, oauth2Client) {
if (err) {
console.log('err:', err);
} else {
sendSampleMail(oauth2Client, function(err, results) {
if (err) {
console.log('err:', err);
} else {
console.log(results);
}
});
}
});
})();

4 changes: 4 additions & 0 deletions package.json
Expand Up @@ -19,5 +19,9 @@
"dependencies": {
"google-auth-library": "^0.9.7",
"googleapis": "^2.1.6"
},
"devDependencies": {
"babel-eslint": "^4.1.6",
"eslint": "^1.10.3"
}
}

0 comments on commit 5f9eaa5

Please sign in to comment.