1

I am trying to use the new Gmail API in a test Android Application. After much battling with the authorization progress I have discovered that specifically, The GoogleAuthUtil.getToken() method does not return a sufficient access token that can be used to access the Gmail API features (will return a 403 error when trying to do any action with the API). That said, the only way I did manage to receive a sufficient token is by doing the authentication process using a WebView and a (Installed application -> Other client ID on the API Console). So in one way it's a win win situation cause I can finally use the API, but the question that remains is what happens when the access token used for the API calls will expire or be compromised? Is there a way to receive a new token without popping out a consent screen? After all the User have already approved the application.

Many thanks.

EDIT: here's how you get a new token using a refresh token: Thanks to @Noam

        httpClient = new DefaultHttpClient();
        httpPost = new HttpPost(address);
        params.add(new BasicNameValuePair("refresh_token", token));
        params.add(new BasicNameValuePair("client_id", client_id));
        params.add(new BasicNameValuePair("client_secret", client_secret));
        params.add(new BasicNameValuePair("grant_type", "refresh_token"));
        httpPost.setHeader("Content-Type", "application/x-www-form-urlencoded");
        httpPost.setEntity(new UrlEncodedFormEntity(params));
        HttpResponse httpResponse = httpClient.execute(httpPost);
        HttpEntity httpEntity = httpResponse.getEntity();
        is = httpEntity.getContent();
TalMihr
  • 1,528
  • 1
  • 15
  • 31

1 Answers1

1

you can use GoogleAuthUtil.getToken() you just need to add the scope "https://www.googleapis.com/auth/gmail.readonly" with builder.addScope().

Noam Wies
  • 54
  • 4
  • 1
    Yeah, Ive tried that but it seems that the token received from the GoogleAuthUtil.getToken() is not good. I am receiving a new token but again this token gives me a 403 error when trying to connect with the Gmail API. Did you manage to properly connect to the Gmail API using android? – TalMihr Sep 07 '14 at 13:05
  • sorry i use this for other scope ( not the mail ). – Noam Wies Sep 07 '14 at 15:12
  • but from my understanding by default if the user approved your app next time the user will not prompted for consent. see https://developers.google.com/accounts/docs/OAuth2UserAgent – Noam Wies Sep 07 '14 at 15:19
  • Thanks. I'll give it another go here. – TalMihr Sep 07 '14 at 15:51
  • Thanks Noam!!!! You have given me the push I needed. The link was exactly what I needed (Although I was using Installed App and not Client Type). Finally figured out how to handle token refresh. Works like a charm now. – TalMihr Sep 07 '14 at 21:01
  • @TalMihr would you be able to post your working code? I feel I'm working through the same issues as you have using the Gmail API in Android and any help with the auth + getting messages would be helpful! – Rohan Sep 08 '14 at 01:28
  • Okay so the procedure is fairly simple. Use this link to create your log in process. It will help you understand how to get initial authorization. http://www.learn2crack.com/2014/01/android-oauth2-webview.html. When you do you will see that you will get a refresh token after aouthorizing. Save this refresh token and use the same http token request to get a new token when you need it. Use this link to understand how to build the request: https://developers.google.com/accounts/docs/OAuth2InstalledApp – TalMihr Sep 08 '14 at 07:25
  • @TalMihr Great! this makes sense. Have you done it with Android's built in accountmanager? Can this token be used with the new Gmail API? – Rohan Sep 08 '14 at 21:26
  • The AccountManager always gave me a token that does not work woth the Gmail API. Only when I requested a refresh token with the http request it worked. I am looking into another angle right now. If it will turn out well. ill post an update. – TalMihr Sep 08 '14 at 21:28