For some reason my state parameters are not being places into the final url on generateAuthUrl().
My client id, secrets, access type, scopes, and redirect_uri are all generated correctly,
using this code:
let url = this.client.generateAuthUrl({
// 'online' (default) or 'offline' (gets refresh_token)
access_type: 'offline',
// If you only need one scope you can pass it as a string
scope: SCOPES,
// Optional property that passes state parameters to redirect URI
state: {foo: "bar", email: emailAddress, "test": "one"}
});
I get
https://accounts.google.com/o/oauth2/auth?access_type=offline&scope=https%3A%2F%2Fmail.google.com%2F&state=&response_type=code&client_id=xxx.apps.googleusercontent.com&redirect_uri=http%3A%2F%2Flocalhost%3A9000%2Foauth2callback%2Fgoogle
It may have something to do with my version of node, but the problem starts with querystring.stringify in oauth2client.js, line 125.
In node 6.3.1, this leads into node core's querystring.js line 210, where the line fields += ks + encode(stringifyPrimitive(v)); doesn't work for an object.
for now setting state as a Base64 string is an ok workaround
Just make sure to stringify and encode state object passed to generateAuthUrl function:
state: encodeURIComponent(JSON.stringify(state)).
Stringifying and enconding state object did the trick as @matkoklaic mentioned.
This should be included in the documentation.
The docs aren't mentioning this at all, and actually give an example passing an object literal, which is misleading:
var url = oauth2Client.generateAuthUrl({
// 'online' (default) or 'offline' (gets refresh_token)
access_type: 'offline',
// If you only need one scope you can pass it as a string
scope: scopes,
// Optional property that passes state parameters to redirect URI
// state: { foo: 'bar' }
});
I am facing the same issue. It doesn't redirect to the callback url, instead it just hangs the page there
The documentation is not in sync with the OpenID documentation: https://developers.google.com/identity/protocols/OpenIDConnect#state-param
It states that the argument should be a string.
Greetings folks! Apologies for the troubles. We've updated the readme to use a string:
https://github.com/google/google-api-nodejs-client/#generating-an-authentication-url
And the reference docs for google-auth-library now point to the proper type as well:
https://google.github.io/google-auth-library-nodejs/interfaces/_auth_oauth2client_.generateauthurlopts.html#state
Please do let me know if there are any other ways we can make this better!
Most helpful comment
Stringifying and enconding
stateobject did the trick as @matkoklaic mentioned.This should be included in the documentation.