Created
July 11, 2016 09:11
-
-
Save ibare/85cdd8ce94a66cfdcdfc1677a022d5fb to your computer and use it in GitHub Desktop.
Google OAuto Example
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
let express = require('express'); | |
let google = require('googleapis'); | |
let app = express(); | |
const CLIENT_ID = 'client-id'; | |
const CLIENT_SECRET = 'client-secret'; | |
const REDIRECT_URL = 'redirect-url'; | |
let OAuth2 = google.auth.OAuth2; | |
let oauth2Client = new OAuth2(CLIENT_ID, CLIENT_SECRET, REDIRECT_URL); | |
let plus = google.plus('v1'); | |
app.use(express.static('public')); | |
/* | |
* Redirect 수신 | |
*/ | |
app.get('/oauth/callback', (req, res) => { | |
oauth2Client.getToken(req.query.code, function (err, tokens) { | |
if (err) { | |
console.error(err); | |
} | |
oauth2Client.setCredentials(tokens); | |
plus.people.get({ userId: 'me', auth: oauth2Client }, (err, response) => { | |
if (err) { | |
return console.error(err); | |
} | |
//TODO: 사용자 정보를 쿠키든 세션이든 저장. 말하자면 로그인 | |
res.redirect('/index.html'); | |
}); | |
}); | |
}); | |
app.post('/login', (req, res) => { | |
res.redirect(oauth2Client.generateAuthUrl({ | |
access_type: 'offline', | |
scope: 'https://www.googleapis.com/auth/plus.me' | |
})); | |
}); | |
app.listen(process.env.PORT || 4000, () => { | |
console.log('ready %s', process.env.PORT || 4000); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment