I am trying to access the Gmail API using an Android application. I have successfully requested and received an access token using all available scope combinations. But it s
Although it's not described clearly, the problem is that the token obtained via GoogleAuthUtil.getToken()
is cached locally and may expire. The 403 error you're receiving is due to an expired token. The correct way to handle this situation is to call GoogleAuthUtil.clearToken()
to remove the locally cached token, and then call GoogleAuthUtil.getToken()
to get a new one.
Alternatively, you can keep track of the last time you requested a token, and if it's been greater than an hour (the default expiry time for these tokens) do the clearToken + getToken calls pre-preemptively.
That error message is deceptive, I believe it also happens when the developer project credentials client isn't correct. For example you picked "web application" when really you want "installed application".
Confirm you're correctly following instructions from: https://developers.google.com/accounts/docs/OAuth2
(Should be "installed application", etc.)
Can you post the oauth client info you have from the developers console? (Type and any other non-confidential info in there, etc.)
You need to set up your Android app key in Google Dev Console.
45:B5:E4:6F:36:AD:0A:98:94:B4:02:66:2B:12:17:F2:56:26:A0:E0;com.example
Your problem will be automatically solved. Be sure to create client id and key with both your debug keystore and release keystore.
Well here's the deal. So far Tokens given by GoogleAuthUtil && AccountManager do not work with the Gmail API. Don't know if it's a Google thing or not but my solution is this:
Create a Client Id (Applicaion->Other) in the API console. Use the Client Id & Client secret to obtain an Access Token through a WebView. Save the result Refresh Token. Now whenever you want to refresh the Access Token use this request:
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();
Works for me. Hope That in the future a valid token will be provided with the GoogleAuthUtil.getToken() option.