Skip to content

Instantly share code, notes, and snippets.

@Igor-Lopes
Last active September 6, 2018 17:18
Show Gist options
  • Save Igor-Lopes/7fe80780710286628e75d28cbb291339 to your computer and use it in GitHub Desktop.
Save Igor-Lopes/7fe80780710286628e75d28cbb291339 to your computer and use it in GitHub Desktop.
Enable Google API Offline Server Side access with server auth code provided by mobile app (https://developers.google.com/identity/sign-in/android/offline-access)
const { google } = require('googleapis')
module.exports = app => {
const controller = {}
controller.auth = async (req, res, next) => {
try {
const oauth2Client = new google.auth.OAuth2(
'YOUR_CLIENT_ID',
'YOUR_CLIENT_SECRET',
'' // Leave empty string for offline access
)
google.options({ auth: oauth2Client })
// Exchange server auth code for OAuth2 credentials
const { tokens } = await oauth2Client.getToken(
req.body.serverAuthCode
)
oauth2Client.setCredentials(tokens)
const plus = google.plus({
version: 'v1',
auth: oauth2Client
})
const people = await plus.people.get({ userId: 'me' })
res.send(people.data)
} catch (ex) {
next(ex)
}
}
return controller
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment