0
Code snippets for Building OAuth 2.0 credentials : 
    Credential credential = new GoogleCredential.Builder().setTransport(httpTransport)
                        .setJsonFactory(jsonFactory)
                        .setClientSecrets(myAppClientID, myAppSecret)
                        .build(); 
    credential.setRefreshToken(userRefreshToken);

I am using Java Library in order to get the Google Analytics Data.

I do have Client ID, Secret and Refresh Token. I am accessing Google Analytics API though this credentials information, My question is, Will Google OAuth 2.0 take care of Access Token Automatically? Or Do i need to handle it manually with some mechanism? If i am not passing access token to this code.

Jay Panchal
  • 75
  • 1
  • 2
  • 11
  • How did you get the refresh token? – Ján Halaša Apr 27 '17 at 12:21
  • @Ján Halaša Using RESTFul Service call. – Jay Panchal Apr 27 '17 at 12:52
  • Have you tried to run the code? Or replace the `setRefreshCode()` with `setAccessToken()`? You should already have an access token - you get all tokens in a single request - access token, refresh token and possibly ID token. – Ján Halaša Apr 27 '17 at 13:32
  • @Ján Halaša With above code i am getting data successfully. I haven't tried by replacing setRefreshCode() with setAccessToken(). As i want to know that, if i continue with the mechanism of only setting "Refresh Code" as mentioned in snippets...does it have any disadvantages? Or Is it like that.. still i need to pass the access code? Or Only Refresh code would be fine? – Jay Panchal Apr 27 '17 at 13:58

1 Answers1

0

From the Credential API doc:

Thread-safe OAuth 2.0 helper for accessing protected resources using an access token, as well as optionally refreshing the access token when it expires using a refresh token.

So if you don't specify an access token, it will be automatically fetched using the refresh token. But since you already have an access token, I would say it's good to set it - it will save the first network call to the /token endpoint.

Ján Halaša
  • 8,167
  • 1
  • 36
  • 36
  • Great! In that case if i am not bit worried about call hit on end-point '/token' then passing only refresh token would be fine... In the next phase, i would optimise these code. And will consider passing Access Token will be the first step. Now one more query is... Is it possible to get Access Token via java client library? If yes, then how? Thanks by the way. for attention, answer and suggestion. :) – Jay Panchal Apr 28 '17 at 04:52
  • I don't have experience with that library, but you could try to call `credential.getAccessToken()`. – Ján Halaša Apr 28 '17 at 07:36