Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
205 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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": [ | ||
] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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); | ||
} | ||
}); | ||
|
||
})(); | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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); | ||
} | ||
}); | ||
|
||
})(); | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
(function() { | ||
"use strict"; | ||
|
||
module.exports = [ | ||
'https://www.googleapis.com/auth/gmail.send' | ||
]; | ||
})(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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); | ||
} | ||
}); | ||
} | ||
}); | ||
})(); | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters