2016-11-12 96 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. 只讀 ,否則刪除的權限並重新添加他們。