Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

google api refresh token returns null with react-google-login

Refresh token is always null, I plowed through the documentation and did serval experiments. But i could not get the refreshToken to renew the access token for offline use.

I'm using react-google-login to get my offline token. I assume this is correctly set up.

Just in case here's the setup:

    <GoogleLogin
        clientId="***"
        buttonText="Login"
        accessType="offline"
        responseType="code"
        scope={"https://www.googleapis.com/auth/calendar"}
        onSuccess={responseGoogle}
        onFailure={responseGoogle}
    />

But when I'm using the authorization code only an access token is returned that is valid for 3600 seconds. I can use the accessToken to access the api. But it seems the refresh token is not provided.

    GoogleTokenResponse tokenResponse =
            new GoogleAuthorizationCodeTokenRequest(
                    new NetHttpTransport(),
                    JacksonFactory.getDefaultInstance(),
                    "https://www.googleapis.com/oauth2/v4/token",
                    clientSecrets.getDetails().getClientId(),
                    clientSecrets.getDetails().getClientSecret(),
                    AUTH_CODE,
                    REDIRECT_URL)
                    .execute();



    String refreshToken = tokenResponse.getRefreshToken();
    String accessToken = tokenResponse.getAccessToken();

Any clue what is wrong?

like image 429
Greg Avatar asked Dec 29 '18 13:12

Greg


Video Answer


1 Answers

It seems it was related to react-google-login... So i was looking at the wrong thing the whole time. Adding approvalPrompt="force" and prompt='consent'seems to fix it.

    <GoogleLogin
        clientId="***"
        buttonText="Login"
        accessType="offline"
        responseType="code"
        approvalPrompt="force"
        prompt='consent'
        scope={"https://www.googleapis.com/auth/calendar"}
        onSuccess={responseGoogle}
        onFailure={responseGoogle}
    />
like image 77
Greg Avatar answered Oct 13 '22 16:10

Greg