Gmail API access using Android

心不动则不痛 提交于 2019-12-18 14:56:25

问题


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 seems that every time I actually try to use the Gmail API command I am getting a 403 exception reading: Access not configured please use Google developers console to activate the api...

Needless to say the API is activated and a client key was created using the correct package name & sha1 code. Same client ID works well with Google Plus features.

Anyone having this issue or succeeded in connecting to the Gmail api from Android?

Thanks

Here's the log:

09-04 21:40:54.014: W/System.err(26717): com.google.api.client.googleapis.json.GoogleJsonResponseException: 403 Forbidden
09-04 21:40:54.014: W/System.err(26717): {
09-04 21:40:54.014: W/System.err(26717):   "code" : 403,
09-04 21:40:54.014: W/System.err(26717):   "errors" : [ {
09-04 21:40:54.014: W/System.err(26717):     "domain" : "usageLimits",
09-04 21:40:54.014: W/System.err(26717):     "message" : "Access Not Configured. Please use Google Developers Console to activate the API for your project.",
09-04 21:40:54.014: W/System.err(26717):     "reason" : "accessNotConfigured"
09-04 21:40:54.014: W/System.err(26717):   } ],
09-04 21:40:54.014: W/System.err(26717):   "message" : "Access Not Configured. Please use Google Developers Console to activate the API for your project."
09-04 21:40:54.014: W/System.err(26717): }

Are there any other API's that need to be enabled in the API Console except Google+ API and Gmail API?

EDIT:

I have found out that using a WebView based authentication will result in an access token that will grant Gmail API access but this is not a valid solution because the token is short lived. As for now the GoogleAuthUtil will grant a token but it's privileges are not sufficient for using the Gmail API. anyone had success in connecting Android with the Gmail API and would like to share?

EDIT2:

Here are snippets of what I am doing:

Getting the token:

token = GoogleAuthUtil.getToken(MainActivity.this, Plus.AccountApi.getAccountName(mGoogleApiClient), scope);

Trying to fetch messages from the Gmail API:

GoogleCredential credential = new GoogleCredential().setAccessToken(token);
JsonFactory jsonFactory = new JacksonFactory();
HttpTransport httpTransport = new NetHttpTransport();

service = new Gmail.Builder(httpTransport, jsonFactory, credential).setApplicationName("GmailApiTP").build();
ListMessagesResponse messagesRespose;
List<Message> m = null;

ArrayList<String> ids = new ArrayList<String>();
ids.add("INBOX");
try {
    messagesRespose = service.users().messages().list("me").setLabelIds(ids).setQ("From: something")
            .execute();
    m = messagesRespose.getMessages();

} catch (IOException e) {
    e.printStackTrace();
}

The exception is caught when use the Gmail API service. In addition I have tried clearing the token and asking for a new one with the same result.


回答1:


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.)




回答2:


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.




回答3:


You need to set up your Android app key in Google Dev Console.

  1. Choose your project, select API & Auth, then click Credentials
  2. Create new client id (though it has other client ids)
  3. Select installed app -> android
  4. Fill in your package name and SHA1 correctly
  5. Create new Key (though it has other client keys)
  6. Select Android key
  7. Fill in the SHA1;packageName like this: 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.




回答4:


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.



来源:https://stackoverflow.com/questions/25668152/gmail-api-access-using-android

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!