Google-api-nodejs-client: pass state parameters with the oauth redirect URI

Created on 14 Dec 2016  路  9Comments  路  Source: googleapis/google-api-nodejs-client

How does one achieve what is described here: http://stackoverflow.com/questions/7722062/google-oauth2-redirect-uri-with-several-parameters# using this package?

Most helpful comment

Because I came here thanks to google search.
Here what I did, at the end:

const oAuth2 = new google.auth.OAuth2(
  process.env.GOOGLE_AUTH_CLIENT_ID,
  process.env.GOOGLE_AUTH_CLIENT_SECRET,
  process.env.GOOGLE_AUTH_REDIRECT,
)

const url = oAuth2.generateAuthUrl({
  access_type: 'offline',
  scope: [
    // all your scopes
  ],
  prompt: 'consent', // ask for a refresh token everytime you need, you can comment it if you don't need it
  state: JSON.stringify({ your: 'informations' }), // needs to be a string

})

All 9 comments

Add a state property to the options passed to OAuth2Client#generateAuthUrl. For example, here you could add state: { 'foo': 'bar' } and I believe the stringified state object will be passed through to the final destination.

doc doc doc

+1 - Please add this to the docs, was searching for hours on a way to pass some form of parameter from my App to the redirect URI and then receive it back again to map the request to the original user.

Because I came here thanks to google search.
Here what I did, at the end:

const oAuth2 = new google.auth.OAuth2(
  process.env.GOOGLE_AUTH_CLIENT_ID,
  process.env.GOOGLE_AUTH_CLIENT_SECRET,
  process.env.GOOGLE_AUTH_REDIRECT,
)

const url = oAuth2.generateAuthUrl({
  access_type: 'offline',
  scope: [
    // all your scopes
  ],
  prompt: 'consent', // ask for a refresh token everytime you need, you can comment it if you don't need it
  state: JSON.stringify({ your: 'informations' }), // needs to be a string

})

Please add this to the docs. Thank you

doc!!!

We really need doc. for this.

Here you go!
https://googleapis.dev/nodejs/google-auth-library/latest/interfaces/GenerateAuthUrlOpts.html#state

TL;DR: you can pass a state property in the options to generateAuthUrl that contains a string which is passed back to your redirect URI after generating a new code :)

Please don't do this:

state: JSON.stringify({ your: 'informations' }), // needs to be a string

The state param needs to be a random string and not predictable or you will be vulnerable to several OAuth2 attack vectors.

Was this page helpful?
0 / 5 - 0 ratings