Skip to content Skip to sidebar Skip to footer

Unable To Exchange Authorization Code For Access Token And Refresh Token In Cross Client Google Oauth2.0

I've been having problems implementing Google Play Services login on my android app and passing the authorisation code to my backend server, so the server will exchange the code fo

Solution 1:

Use from this:

import com.google.api.client.googleapis.auth.oauth2.GoogleAuthorizationCodeTokenRequest;
import com.google.api.client.googleapis.auth.oauth2.GoogleIdToken;
import com.google.api.client.googleapis.auth.oauth2.GoogleTokenResponse;
import com.google.api.client.http.HttpTransport;
import com.google.api.client.http.javanet.NetHttpTransport;
import com.google.api.client.json.jackson.JacksonFactory;


privatefinal val TRANSPORT: HttpTransport = newNetHttpTransport()
    privatefinal val JSON_FACTORY: JacksonFactory = newJacksonFactory()


GoogleTokenResponsetokenResponse=newGoogleAuthorizationCodeTokenRequest(TRANSPORT, JSON_FACTORY,
                CLIENT_ID, CLIENT_SECRET, code, "postmessage").execute();

GoogleIdTokenidToken= tokenResponse.parseIdToken();
StringgplusId= idToken.getPayload().getSubject();

you must replace your values of the client_id,client_secret and code with above related variables.

more information is in the flowing link: https://github.com/googleplus/gplus-quickstart-java/blob/master/src/com/google/plus/samples/quickstart/Signin.java

Also you can get the Google API libraries from this link:

http://repo1.maven.org/maven2/com/google/

Solution 2:

You need to exchange the code for an access token indeed. The id_token is meant for your client only and self-contained so it does not need to be exchanged for anything else. The redirect_uri that you need to send along with the code in the exchange request to the token endpoint is the exact same redirect_uri value that you sent originally in the authorization request that went to the authorization endpoint (and that is registered for your client in the Google API Console), so looking at your code it is the one retrieved with clientSecrets.getDetails().getRedirectUris().get(0);

Post a Comment for "Unable To Exchange Authorization Code For Access Token And Refresh Token In Cross Client Google Oauth2.0"