2
votes

I've been trying to figure this out for awhile now, cant seem to get with anything I've tried.

I cant seem to retrieve the refresh token, it would come out as blank.

 public class oaUTH extends AsyncTask<Void, Void, Void>  {



    protected Void doInBackground(Void... unused) {

        try {
            JsonFactory jsonFactory = new JacksonFactory();
            HttpTransport transport = new NetHttpTransport();

            CredentialStore credentialStore = new SharedPreferencesCredentialStore(prefs);
            AccessTokenResponse accessTokenResponse = credentialStore.read();

            GoogleAccessProtectedResource accessProtectedResource = new GoogleAccessProtectedResource(accessTokenResponse.accessToken,
                                                                                                                            transport,
                                                                                                                            jsonFactory,
                                                                                                                            OAuth2ClientCredentials.CLIENT_ID,
                                                                                                                            OAuth2ClientCredentials.CLIENT_SECRET,
                                                                                                                            accessTokenResponse.refreshToken+"&access_type=offline");       
            final Latitude latitude = new Latitude(transport, accessProtectedResource, jsonFactory);
            latitude.apiKey=OAuth2ClientCredentials.API_KEY;
            Log.i("AGENTPORTAL", "Access Token = " + accessTokenResponse.accessToken + ", Expires in = " + accessTokenResponse.expiresIn + ", Refresh Token = " + accessTokenResponse.refreshToken + ", Scope = " + accessTokenResponse.scope);

            LatitudeCurrentlocationResourceJson currentLocation = latitude.currentLocation.get().execute();
            String locationAsString = convertLocationToString(currentLocation);

            Log.i("TAG", locationAsString);
            //textView.setText(locationAsString);



        } catch (Exception ex) {
            ex.printStackTrace();
            //textView.setText("Error occured : " + ex.getMessage());
            startActivity(new Intent().setClass(getApplicationContext(),OAuthAccessTokenActivity.class));
        }

Here is the log: Access Token = **AHES6ZTeqgwdxjll6Gb4Cf9I0_n5bO_OdgR2OR**WLPCPzJ5xtO5M, Expires in = 3599, Refresh Token = , Scope =

Any idea where refresh token = " " ? I've added "&access_type=offline" but still no luck. Any help is deeply appreciated!

1

1 Answers

3
votes

I just figured out how to get the refresh token, I'll tell you what I did just in case someone ever runs into the same problem...

        String authorizationUrl = new GoogleAuthorizationRequestUrl(        
            OAuth2ClientCredentials.CLIENT_ID,
            OAuth2ClientCredentials.REDIRECT_URI,
            OAuth2ClientCredentials.SCOPE).build();

That piece of code is what is first called to start the whole authorization process. Whats missing in it is what several people here on SO had suggested, you need to include "access_type=offline&approval_prompt=force" in the URL link.

To make it work, I simply changed the authorizationUrl to

                    String authorizationUrl = "https://accounts.google.com/o/oauth2/auth?access_type=offline&approval_prompt=force&client_id=" + OAuth2ClientCredentials.CLIENT_ID + "&redirect_uri=" + OAuth2ClientCredentials.REDIRECT_URI + "&response_type=code&scope=" + OAuth2ClientCredentials.SCOPE