Created
July 4, 2018 19:17
-
-
Save jackrobertscott/82fdfa2191b8e5d74123ac30cbb5e8c2 to your computer and use it in GitHub Desktop.
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
import { google } from 'googleapis'; | |
/*******************/ | |
/** CONFIGURATION **/ | |
/*******************/ | |
const googleConfig = { | |
clientId: process.env.GOOGLE_CLIENT_ID, // e.g. asdfghjkljhgfdsghjk.apps.googleusercontent.com | |
clientSecret: process.env.GOOGLE_CLIENT_SECRET, // e.g. _ASDFA%DFASDFASDFASD#FAD- | |
redirect: process.env.GOOGLE_REDIRECT_URL, // this must match your google api settings | |
}; | |
const defaultScope = [ | |
'https://www.googleapis.com/auth/plus.me', | |
'https://www.googleapis.com/auth/userinfo.email', | |
]; | |
/*************/ | |
/** HELPERS **/ | |
/*************/ | |
function createConnection() { | |
return new google.auth.OAuth2( | |
googleConfig.clientId, | |
googleConfig.clientSecret, | |
googleConfig.redirect | |
); | |
} | |
function getConnectionUrl(auth) { | |
return auth.generateAuthUrl({ | |
access_type: 'offline', | |
prompt: 'consent', | |
scope: defaultScope | |
}); | |
} | |
function getGooglePlusApi(auth) { | |
return google.plus({ version: 'v1', auth }); | |
} | |
/**********/ | |
/** MAIN **/ | |
/**********/ | |
/** | |
* Part 1: Create a Google URL and send to the client to log in the user. | |
*/ | |
function urlGoogle() { | |
const auth = createConnection(); | |
const url = getConnectionUrl(auth); | |
return url; | |
} | |
/** | |
* Part 2: Take the "code" parameter which Google gives us once when the user logs in, then get the user's email and id. | |
*/ | |
function getGoogleAccountFromCode(code) { | |
const data = await auth.getToken(code); | |
const tokens = data.tokens; | |
const auth = createConnection(); | |
auth.setCredentials(tokens); | |
const plus = getGooglePlusApi(auth); | |
const me = await plus.people.get({ userId: 'me' }); | |
const userGoogleId = me.data.id; | |
const userGoogleEmail = me.data.emails && me.data.emails.length && me.data.emails[0].value; | |
return { | |
id: userGoogleId, | |
email: userGoogleEmail, | |
tokens: tokens, | |
}; | |
} |
sorry man it is solved my mistake in typo
buttttt
that google+ is discarded it is not supported now
So
how i can get email and other info
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Error: invalid_request for 59 line
I have passed code query from url