Created
May 12, 2011 08:09
-
-
Save dekz/968144 to your computer and use it in GitHub Desktop.
google auth
This file contains hidden or 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
| https = require 'https' | |
| http = require 'http' | |
| querystring = require 'querystring' | |
| fs = require 'fs' | |
| settings = { | |
| clientID : '.apps.googleusercontent.com', | |
| clientSecret : '', | |
| redirectURI : 'urn:ietf:wg:oauth:2.0:oob', | |
| code : '', | |
| refreshToken : '', | |
| expiresIn : 3600 , | |
| accessToken : '' | |
| } | |
| getAccessToken = -> | |
| body = { 'client_id' : settings.clientID, 'client_secret': settings.clientSecret, 'code': settings.code, 'redirect_uri': settings.redirectURI, 'grant_type': 'authorization_code'} | |
| queryGoogle('POST', body, 'www.google.com', '/accounts/o8/oauth2/token', (resp) -> | |
| resp = JSON.parse resp | |
| console.log resp | |
| refreshToken = resp['refresh_token'] | |
| expiresIn = resp['expires_in'] | |
| accessToken = resp['access_token'] | |
| ) | |
| getRefreshToken = -> | |
| body = { 'client_id': settings.clientID, 'client_secret': settings.clientSecret, 'refresh_token': settings.refreshToken, 'grant_type': 'refresh_token' } | |
| console.log body | |
| queryGoogle('POST', body, 'accounts.google.com', '/o/oauth2/token', (resp) -> | |
| console.log resp | |
| ) | |
| queryGoogle = (type, data, host, path, cb) -> | |
| body = querystring.stringify(data, '&', '=') | |
| console.log body | |
| options = { | |
| host: host, | |
| port: 443, | |
| method: type | |
| path: path, | |
| headers: { | |
| 'Content-Length': body.length, | |
| 'Content-Type': 'application/x-www-form-urlencoded' | |
| } | |
| } | |
| req = https.request(options, (res) -> | |
| status = res.statusCode | |
| responseBody = '' | |
| res.setEncoding('utf8') | |
| res.on('data', (chunk) -> | |
| responseBody += chunk | |
| ) | |
| res.on('end', -> | |
| cb responseBody | |
| ) | |
| ) | |
| req.write(body, 'utf8') | |
| req.end() | |
| saveSettings = -> | |
| fs.writeFileSync 'settings.json', JSON.stringify(settings), encoding='utf8' | |
| readSettings = -> | |
| settings = JSON.parse(fs.readFileSync('settings.json')) | |
| begin = -> | |
| #getAccessToken() | |
| readSettings() | |
| getRefreshToken() | |
| saveSettings() | |
| begin() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment