hi.
Sometimes access_token is not defined after logging in.
So I debugged and found that function getAuthResponse, which is used in the library, was returning undefined.
The official document states that access_token is not returned when:
By default, the access token and requested scopes are not returned when fetched_basic_profile is true (the default value) and no additional scopes are required.
I have to specify fetched_basic_profile as true, and scope as profile email.
The default value for this library is the same as the value I should use, so I didn’t specify it in the code.
I am using the login component as below.
<GoogleLogin
render={LoginButton}
clientId={googleClientId}
onRequest={onGoogleLoginRequest}
onSuccess={onGoogleLoginSuccess}
onFailure={onGoogleLoginFailure}
cookiePolicy=“single_host_origin”
/>
To solve this problem, I would like to modify the parameter of function getAuthResponse.
Or is there another better solution?
Now the tc field in res somehow has been changed to wc. According to https://github.com/anthonyjgrove/react-google-login/issues/17, this property name is unreliable.
I notice that there is a field tc always contains access_token when the access_token is undefined. I use it as an alternative.
Im getting accessToken: undefined in the main object and another object inside called wc which contains access_token.
also tried using getAuthResponse and it did not return an access token
Did anyone find a solution for that?
@Sultanyaron As the README and GoogleUser object said, You should add true as
a parameter to getAuthResponse().
// You can always get the `access_token` in this way
res.getAuthResponse(true).access_token;
getAuthResponse doesn't accept any arguments as per https://github.com/anthonyjgrove/react-google-login/blob/master/index.d.ts#L30
While calling res.getAuthResponse().access_token returns undefined for me
@Sultanyaron As the README and GoogleUser object said, You should add
trueas
a parameter togetAuthResponse().// You can always get the `access_token` in this way res.getAuthResponse(true).access_token;
Can confirm that this properly sets the access_token. @anthonyjgrove should we open a PR for this? At least set it to true when access_type is token
@phanshiwen
It is working properly and i am getting the token with the following:
//@ts-ignore
const googleAccessToken = loginResponse.getAuthResponse(true).access_token;
According to the definition file getAuthResponse don't accept args, this is something that need to get fixed.
Edit
Now the
tcfield inressomehow has been changed towc. According to #17, this property name is unreliable.I notice that there is a field
tcalways containsaccess_tokenwhen theaccess_tokenisundefined. I use it as an alternative.
Now the wc has changed to xc
Is this due to a change in Google's response?
Most helpful comment
@Sultanyaron As the README and GoogleUser object said, You should add
trueasa parameter to
getAuthResponse().