2016-11-12 70 views
3

我正在尝试为android编写小型Gmail客户端作为培训。 我把https://developers.google.com/gmail/api/quickstart/android的gmail api指南样本修改了一下,以便通过线程获得标题为&的消息。我设置范围至GmailScopes.Gmail_modify和编辑主请求的功能,因为这:Gmail API范围和格式不匹配

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

回答

4

这正是我在使用Google APIs Explorer时遇到的问题。这里是我做过什么来解决这个问题:

  1. 转到https://security.google.com/settings/security/permissions
  2. 选择你正在玩的应用程序,我的是谷歌API浏览器
  3. 点击删除>确定
  4. 下一次,只是请求确切地说你需要哪些权限。

希望它能帮助:)

0

获得设备联系人权限后,您必须批准选定的应对方式。所以我第一次批准元数据范围。当我需要批准只读范围的时候,没有任何窗口可以做到。因此,您需要从Google帐户中删除范围权限并重新安装应用。

+0

你的意思是你从管理控制台删除你的范围并重新添加它们?我面临同样的问题。 – jpo

0

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

检查应用程序的权限,以确保你只有这3个领域:

  1. https://mail.google.com/
  2. gmail.modify
  3. 只读 ,否则删除的权限并重新添加他们。