簡體   English   中英

Android Gmail API 獲取消息 403 錯誤

[英]Android Gmail API get message 403 error

為 Android 實現 Gmail API 時出錯。 我的范圍:

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

我可以獲取消息 ID:

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

在 Android 監視器中:

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

但是當我嘗試收到某些消息時:

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;
}

我收到一個錯誤:

發生以下錯誤:403 Forbidden { "code" : 403, "errors" : [ { "domain" : "global", "message" : "Metadata scope does not allow format FULL", "reason" : "forbidden" } ], "message" : "元數據范圍不允許格式為FULL" }

當我在新項目中運行我的代碼時,一切正常。 為什么?

您應該刪除“元數據”范圍。

檢查應用權限以確保您只有以下 3 個范圍:

  1. https://mail.google.com/
  2. gmail.修改
  3. 只讀

, 否則刪除權限並重新添加。

這是 Gmail API 中另一個令人失望的設計。 我通過以下步驟解決了這個問題:

  1. 從范圍中刪除 GmailScopes.GMAIL_METADATA。
  2. 從本地文件系統中刪除“StoredCredential”文件。 此文件由 Google 的 GoogleAuthorizationCodeFlow 實例在調用 setDataStoreFactory 以存儲您的憑據時生成。
  3. 通過再次登錄https://accounts.google.com/o/oauth2/auth?access_type=offline重新授權您的應用......

希望這可以幫助你們。

嘗試將GmailScopes.GMAIL_METADATA添加到范圍或只使用GmailScopes.all()方法

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM