I am using https://github.com/react-native-google-signin/google-signin ,in my UI from here i have got serveAuthCode, But i am not able to figure out how get accesstoken from this serverAuthCode in java backend. Any help would be greatly appreciated.
similar issue : link
in my case i was getting serverAuthCode from https://github.com/react-native-google-signin/google-signin,
in the backend then you can do below code
import com.google.api.client.googleapis.auth.oauth2.GoogleAuthorizationCodeTokenRequest;
import com.google.api.client.googleapis.auth.oauth2.GoogleTokenResponse;
import com.google.api.client.googleapis.auth.oauth2.GoogleCredential;
public static final HttpTransport HTTP_TRANSPORT = new NetHttpTransport();
public static final JsonFactory JSON_FACTORY = new JacksonFactory();
GoogleTokenResponse response = new GoogleAuthorizationCodeTokenRequest(Auth.HTTP_TRANSPORT, Auth.JSON_FACTORY,
Your_OAUTH_CLIENTID,
Your_OAUTH_CLIENTSECRET,
Serverauthcode_from_react_native, "")
.execute();
//below code gets you access token of longer expiration date
GoogleCredential refreshTokenCredential = new GoogleCredential.Builder()
.setJsonFactory(Auth.JSON_FACTORY)
.setTransport(Auth.HTTP_TRANSPORT)
.setClientSecrets(Your_OAUTH_CLIENTID, Your_OAUTH_CLIENTSECRET)
.build()
.setRefreshToken(response.getRefreshToken());
refreshTokenCredential.refreshToken();
String newAccessToken = refreshTokenCredential.getAccessToken();
Note: The serverAuthCode can be used only once , from the response that you get you should store the refreshToken, and from next time use refreshToken to get you AccessToken