简体   繁体   中英

Android Gmail API get message 403 error

I have error when implement Gmail API for Android. My Scopes:

private static final String[] SCOPES = { GmailScopes.GMAIL_LABELS, GmailScopes.GMAIL_COMPOSE,
            GmailScopes.GMAIL_INSERT, GmailScopes.GMAIL_MODIFY, GmailScopes.GMAIL_READONLY, GmailScopes.MAIL_GOOGLE_COM };

I can to get messages Id:

Log.i("messages.get(0)", messages.get(0).toPrettyString());

In Android Monitor:

I/messages.get(0): {"id": "1588ae7e991a62f2","threadId" : "1588ae7e991a62f2"}

But when I try to get certain message:

getGmailMessage(mService, user, "1588ae7e991a62f2");

public static Message getGmailMessage(Gmail service, String userId, String messageId) throws IOException {
    Message message = service.users().messages().get(userId, messageId).execute();
    System.out.println("Message snippet: " + message.getSnippet());
    System.out.println("Message getHeaders: " + message.getPayload().getHeaders().get(0));
    System.out.println("Message toPrettyString: " + message.getPayload().toPrettyString());
    return message;
}

I get an error:

The following error occurred: 403 Forbidden { "code" : 403, "errors" : [ { "domain" : "global", "message" : "Metadata scope doesn't allow format FULL", "reason" : "forbidden" } ], "message" : "Metadata scope doesn't allow format FULL" }

When I run my code in a new project, everything works. Why?

you should remove the "metadata" scope.

check app permissions to make sure you have only these 3 scopes:

  1. https://mail.google.com/
  2. gmail.modify
  3. readonly

, or else remove the permissions and add them again.

This is another disappointing design in the Gmail API. I resolved this issue with the following steps:

  1. Remove GmailScopes.GMAIL_METADATA from scopes.
  2. Remove the 'StoredCredential' file from your local FileSystem. This file is generated by Google's GoogleAuthorizationCodeFlow instance when the setDataStoreFactory gets call to store your credential.
  3. Re authorize your app by signing in again https://accounts.google.com/o/oauth2/auth?access_type=offline ....

Hope this helps you guys out.

尝试将GmailScopes.GMAIL_METADATA添加到范围或只使用GmailScopes.all()方法

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM