function getAuthenticationUrl() { var client = new google.auth.OAuth2( config.oauth2.clientId, config.oauth2.clientSecret, config.oauth2.redirectUrl ); // Use 'profile' scope to authorize fetching the user's profile return client.generateAuthUrl({ scope: ['profile'] }); }
async authorize(credentials) { const {client_secret, client_id, redirect_uris} = credentials.installed const oAuth2Client = new google.auth.OAuth2( client_id, client_secret, redirect_uris[0]) // Check if we have previously stored a token. try { let token = await fs.readFile(this.token_path) oAuth2Client.setCredentials(JSON.parse(token)) } catch (err) { let res = await this.getAccessToken(oAuth2Client) return res } return oAuth2Client }
const genAuthUrl = (options, stateUrl) => { const client = getClient(options); const opts = { access_type: 'offline', scope: AUTH_SCOPES, state: stateUrl, }; if (isV2AuthApi(google.auth.OAuth2.GOOGLE_OAUTH2_AUTH_BASE_URL_)) { opts.prompt = 'consent'; } else { opts.approval_prompt = 'force'; } return client.generateAuthUrl(opts); }
const getClient = options => { return new google.auth.OAuth2( options.client_id, options.client_secret, options.redirect_url ); }
function getUser(authorizationCode, callback) { var client = new google.auth.OAuth2( config.oauth2.clientId, config.oauth2.clientSecret, config.oauth2.redirectUrl ); // With the code returned from OAuth flow, get an access token client.getToken(authorizationCode, function(err, tokens) { if (err) return callback(err); // Configure this Google API client to use the access token client.setCredentials(tokens); // Call the Google+ API to get the profile of the user who authenticated google.plus('v1').people.get({ userId: 'me', auth: client }, function(err, profile) { if (err) return callback(err); var user = { id: profile.data.id, name: profile.data.displayName, imageUrl: profile.data.image.url }; callback(null, user); }); }); }