Created
August 22, 2012 06:54
-
-
Save tpmo/3423110 to your computer and use it in GitHub Desktop.
Request and save an Google oAuth2 request_token (node.js, express)
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
var express = require( 'express' ) | |
, OAuth2 = require( 'oauth' ).OAuth2 | |
, querystring = require( 'querystring' ) | |
, request = require( 'request' ) | |
, fs = require('fs'); | |
var oa2; | |
var app = express.createServer(); | |
app.use(express.logger()); | |
app.use(express.bodyParser()); | |
app.use(express.cookieParser()); | |
app.use(express.session({ | |
secret: "secret" | |
})); | |
app.get('/', function(req, res) { | |
oa2.getOAuthAccessToken( req.query[ 'code' ], { grant_type: 'authorization_code', redirect_uri: 'http://localhost:9004' }, function( error, access_token, refresh_token, results ) { | |
if ( !error ) { | |
fs.writeFile( './token/refresh_token', refresh_token, function( err ) { | |
if(err) { | |
console.log( err ); | |
} | |
else { | |
console.log( 'Refresh token saved!' ); | |
} | |
}); | |
} | |
}); | |
}); | |
app.get('/google_login', function(req, res) { | |
var getRequestTokenUrl = ''; | |
oa2 = new OAuth2( | |
'clientId' | |
, 'clientSecret' | |
, 'https://accounts.google.com' | |
, '/o/oauth2/auth' | |
, '/o/oauth2/token' | |
); | |
var params = { | |
, 'response_type': 'code' | |
, 'scope': 'https://www.googleapis.com/auth/calendar' | |
, 'redirect_uri': 'http://localhost:9004' | |
} | |
redirectUrl = oa2.getAuthorizeUrl( params ); | |
res.redirect( redirectUrl ); | |
}); | |
app.listen(9004); | |
console.log("listening on http://localhost:9004"); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment