我正在嘗試使用Android應用程序訪問Gmail API。 我已經使用所有可用的範圍組合成功請求並接收了一個訪問令牌。 但似乎每一次我真正嘗試使用Gmail API命令我得到一個403 exception reading: Access not configured please use Google developers console to activate the api...
使用Android的Gmail API訪問
不用多說了API被激活和客戶端密鑰,用正確的軟件包名稱& SHA1代碼創建的。相同的客戶端ID與Google Plus功能配合良好。
任何有此問題或成功連接到Android的Gmail api的人?
感謝
這裏的日誌:
09-04 21:40:54.014: W/System.err(26717): com.google.api.client.googleapis.json.GoogleJsonResponseException: 403 Forbidden
09-04 21:40:54.014: W/System.err(26717): {
09-04 21:40:54.014: W/System.err(26717): "code" : 403,
09-04 21:40:54.014: W/System.err(26717): "errors" : [ {
09-04 21:40:54.014: W/System.err(26717): "domain" : "usageLimits",
09-04 21:40:54.014: W/System.err(26717): "message" : "Access Not Configured. Please use Google Developers Console to activate the API for your project.",
09-04 21:40:54.014: W/System.err(26717): "reason" : "accessNotConfigured"
09-04 21:40:54.014: W/System.err(26717): } ],
09-04 21:40:54.014: W/System.err(26717): "message" : "Access Not Configured. Please use Google Developers Console to activate the API for your project."
09-04 21:40:54.014: W/System.err(26717): }
是否有需要在API控制檯啓用Google+的除外API和Gmail API的任何其他的API?
編輯:
我發現,使用基於WebView認證將導致一個訪問令牌,將授予Gmail的API訪問但這不是一個有效的解決方案,因爲令牌短暫的。至於GoogleAuthUtil現在將授予令牌,但它的權限不足以使用Gmail API。 任何人都可以成功地將Android與Gmail API連接,並希望分享?
EDIT2:
下面是摘錄我在做什麼:
獲取令牌:
token = GoogleAuthUtil.getToken(MainActivity.this, Plus.AccountApi.getAccountName(mGoogleApiClient), scope);
嘗試獲取來自Gmail的API消息:
GoogleCredential credential = new GoogleCredential().setAccessToken(token);
JsonFactory jsonFactory = new JacksonFactory();
HttpTransport httpTransport = new NetHttpTransport();
service = new Gmail.Builder(httpTransport, jsonFactory, credential).setApplicationName("GmailApiTP").build();
ListMessagesResponse messagesRespose;
List<Message> m = null;
ArrayList<String> ids = new ArrayList<String>();
ids.add("INBOX");
try {
messagesRespose = service.users().messages().list("me").setLabelIds(ids).setQ("From: something")
.execute();
m = messagesRespose.getMessages();
} catch (IOException e) {
e.printStackTrace();
}
使用Gmail API服務時會發生異常。此外,我試圖清除令牌並要求有相同結果的新令牌。
您是否嘗試實施此示例(https://developers.google.com/gmail/api/quickstart/quickstart-java#step_1_enable_the_gmail_api)? – 2014-09-13 06:12:54
是的。這很簡單。但仍然無法解釋爲什麼GooglaApiCient會返回不可用的令牌。 – TalMihr 2014-09-18 22:06:34
就像一個數據點一樣,'GoogleAuthUtil'對我使用Gmail API從Jelly Bean和KitKat上的服務發送電子郵件的工作正常。我直接使用REST API(即通過'HttpURLConnection')而不是Java客戶端庫,但這應該獨立於令牌獲取。我得到了你的403一段時間,直到我正確設置了Android應用程序的客戶端ID,但在此之後自動刷新處理就沒問題了。一個可能出錯的地方是你沒有顯示的是在'getToken()'調用中處理'UserRecoverableAuthExtension'。 – rhashimoto 2014-10-25 00:54:15