简体   繁体   English

Android Gmail API 获取消息 403 错误

[英]Android Gmail API get message 403 error

I have error when implement Gmail API for Android.为 Android 实现 Gmail API 时出错。 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:我可以获取消息 ID:

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

In Android Monitor:在 Android 监视器中:

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" }发生以下错误:403 Forbidden { "code" : 403, "errors" : [ { "domain" : "global", "message" : "Metadata scope does not allow format FULL", "reason" : "forbidden" } ], "message" : "元数据范围不允许格式为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:检查应用权限以确保您只有以下 3 个范围:

  1. https://mail.google.com/ https://mail.google.com/
  2. gmail.modify gmail.修改
  3. readonly只读

, or else remove the permissions and add them again. , 否则删除权限并重新添加。

This is another disappointing design in the Gmail API.这是 Gmail API 中另一个令人失望的设计。 I resolved this issue with the following steps:我通过以下步骤解决了这个问题:

  1. Remove GmailScopes.GMAIL_METADATA from scopes.从范围中删除 GmailScopes.GMAIL_METADATA。
  2. Remove the 'StoredCredential' file from your local FileSystem.从本地文件系统中删除“StoredCredential”文件。 This file is generated by Google's GoogleAuthorizationCodeFlow instance when the setDataStoreFactory gets call to store your credential.此文件由 Google 的 GoogleAuthorizationCodeFlow 实例在调用 setDataStoreFactory 以存储您的凭据时生成。
  3. Re authorize your app by signing in again https://accounts.google.com/o/oauth2/auth?access_type=offline ....通过再次登录https://accounts.google.com/o/oauth2/auth?access_type=offline重新授权您的应用......

Hope this helps you guys out.希望这可以帮助你们。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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