Gmail api范围和格式不匹配

Gre*_*een 4 java android oauth-2.0 gmail-api

我正在尝试为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;
        }
Run Code Online (Sandbox Code Playgroud)

但我总是得到

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"
                                                                           }
Run Code Online (Sandbox Code Playgroud)

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

Nha*_*han 7

这是使用Google API Explorer玩这一天的问题.以下是我为解决这个问题所做的工作:

  1. 转到https://security.google.com/settings/security/permissions
  2. 选择您正在玩的应用程序,我的是Google API Explorer
  3. 单击删除>确定
  4. 下次,只需要确切地请求您需要的权限.

希望它有帮助:)


小智 6

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

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

  1. https://mail.google.com/
  2. gmail.修改
  3. readonly ,否则删除权限并再次添加它们。