1

I am using Scribe for oauth to get access_token and refresh_token for the auth_code. It worked well for the first authentication. When I disabled the credentials in my application, the tokens are still existing with the Gmail connected apps for my application. When I re-enable the oauth, I am getting only valid new access_token for the new auth_code. But the refresh_token is null. I tried to replicate the same with oauthplayground, there I could see valid refresh and access tokens, while playground tokens still existed in the google connected apps.

here is the code I implemented with scribe-1.3.0 API

    OAuthRequest oAuthRequest = new OAuthRequest(Verb.POST, "https://accounts.google.com/o/oauth2/token");

    oAuthRequest.addBodyParameter("client_id", SMTP_OAUTH_CLIENT_ID);
    oAuthRequest.addBodyParameter("client_secret", SMTP_OAUTH_CLIENT_SECRET);

    oAuthRequest.addBodyParameter("scope", scope);
    oAuthRequest.addBodyParameter("redirect_uri", GoogleApi.getRedirectURL());

    oAuthRequest.addBodyParameter("code", authCode);
    oAuthRequest.addBodyParameter("grant_type", "authorization_code");

    Response response = oAuthRequest.send();

Here is the similar code tried with googleapi-client-1.20, the result is still same.

        GoogleTokenResponse tokenResponse = new GoogleAuthorizationCodeTokenRequest(
                new NetHttpTransport(), 
                new JacksonFactory(), 
                SMTP_OAUTH_CLIENT_ID, SMTP_OAUTH_CLIENT_SECRET, 
                authCode, 
                GoogleApi.getRedirectURL())
                    .execute();

        Credential gcredential = new GoogleCredential
                .Builder()
                .setTransport(new NetHttpTransport.Builder().build())
                .setJsonFactory(new JacksonFactory())
                .setClientSecrets(SMTP_OAUTH_CLIENT_ID, SMTP_OAUTH_CLIENT_SECRET)
                .build()
                .setFromTokenResponse(tokenResponse);

        gcredential.refreshToken();

        System.out.println(gcredential.getRefreshToken());

Can anyone help me where I am going wrong? Thanks for your time in looking at this issue.

1 Answer 1

1

When re-authenticating you need to set these parameters access_type=offline and approval_prompt=force then you will recieve refresh_token

Below is an example URL https://accounts.google.com/o/oauth2/auth?client_id=CLIENT_ID&scope=SCOPE&redirect_uri=REDIRECT_URL&response_type=code&access_type=offline&approval_prompt=force

1
  • SUPER. IT WORKED.
    – ravi.t
    Dec 6, 2016 at 3:44

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.