【问题标题】:Gmail api scope & format mismatchGmail api 范围和格式不匹配
【发布时间】:2017-03-26 18:05:13
【问题描述】:

我正在尝试为 android 编写小型 gmail 客户端作为培训。 我从https://developers.google.com/gmail/api/quickstart/android 获取了 gmail api 指南示例,对其进行了一些修改,以便按线程获取带有标题和正文的消息。我将范围设置为GmailScopes.Gmail_modify 并编辑了主请求函数:

private List<String> getDataFromApi() throws IOException {
            // Get the labels in the user's account.
            String user = "me";
            List<String> labels = new ArrayList<String>();
            ListLabelsResponse listResponse =
                    mService.users().labels().list(user).execute();
            ListThreadsResponse listThreads = null;
            try {
             listThreads = mService.users().threads().list(user).execute();
            } catch (IOException ioex){
                Log.e(LOG_TAG, "First: " + ioex.toString());
            }

            for (Thread thread : listThreads.getThreads()) {
                try {
                    thread = mService.users().threads().get(user, thread.getId()).setFormat("full").execute();
                } catch (IOException ioex){
                    Log.e(LOG_TAG, "Second: " + ioex.toString());
                }
                for(Message message : thread.getMessages()){
                    labels.add(message.getId());
                }

            }
            return labels;
        }

但我总是得到

Second: GoogleJsonResponseException: 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"
                                                                           }

我尝试了不同的范围配置,但似乎服务范围始终设置为 GmailScopes.GMAIL_METADATA

【问题讨论】:

    标签: java android oauth-2.0 gmail-api


    【解决方案1】:

    这正是我这些天在使用 Google APIs Explorer 时遇到的问题。这是我为解决它所做的:

    1. 转到https://security.google.com/settings/security/permissions
    2. 选择您正在玩的应用,我的是 Google APIs Explorer
    3. 点击删除 > 确定
    4. 下一次,只需准确请求您需要的权限即可。

    希望对你有帮助:)

    【讨论】:

    • 要检查的确切权限是什么?我找不到有效的组合。
    【解决方案2】:

    您应该删除“元数据”范围。

    检查应用权限以确保您只有这 3 个范围:

    1. https://mail.google.com/
    2. gmail.modify
    3. 只读 ,或者删除权限并重新添加。

    【讨论】:

    • 2020 年仍然有效,这应该是公认的答案。谢谢!
    【解决方案3】:

    在获得设备联系人的权限后,您必须批准所选的副本。所以我第一次批准了元数据范围。下次当我需要批准只读范围时,没有窗口可以这样做。因此,您需要从谷歌帐户中删除范围权限并重新安装应用程序。

    【讨论】:

    • 您的意思是从管理控制台中删除您的范围并重新添加它们?我也面临同样的问题。
    猜你喜欢
    • 2015-05-18
    • 2020-04-06
    • 1970-01-01
    • 2020-06-05
    • 2018-04-13
    • 1970-01-01
    • 1970-01-01
    • 2018-06-25
    • 1970-01-01
    相关资源
    最近更新 更多