簡體   English   中英

在Android中使用Gmail API和OAuth2檢查來自Gmail的新電子郵件

[英]Check for new emails from Gmail with Gmail API and OAuth2 in Android

我真的走到了盡頭。 我一直試圖弄清楚如何使用我之前從Google收到的現有OAuth2令牌從Android應用程序到達我的Gmail收件箱。 我可以用一個令牌來證明自己:

URL url = new URL("https://www.googleapis.com/oauth2/v1/userinfo?access_token=" +token);
HttpURLConnection con = (HttpURLConnection) url.openConnection();

但感覺就像我以某種方式以錯誤的角度解決這個問題。 我一直試圖破譯Gmail的API,但沒有得到任何結果。 有人能把我推向正確的方向嗎?

或者,如果我發布的代碼是正確的,我將如何繼續?

編輯:所以我已經找到了如何使用Gmail的API來獲取最新的100封郵件與此代碼:

HttpTransport httpTransport = new NetHttpTransport();
JsonFactory jsonFactory = new JacksonFactory();
GoogleTokenResponse response = new GoogleTokenResponse();
response.setAccessToken(myExistingToken);
GoogleCredential credential = new GoogleCredential().setFromTokenResponse(response);
Gmail service = new Gmail.Builder(httpTransport, jsonFactory, credential).setApplicationName(myApplicationName).build();
ListThreadsResponse threadsResponse = service.users().threads().list("voxcommunis@gmail.com").execute();
List<com.google.api.services.gmail.model.Thread> threads = threadsResponse.getThreads();
for ( com.google.api.services.gmail.model.Thread thread : threads ) {
    Log.d(LOG_TAG, "Thread ID: "+ thread.getId());
}

所以我將繼續在這條路上獲取新的電子郵件如何=)

退房: 使用Android 處理Android中過期的訪問令牌,無需用戶專注Gmail API訪問

他們只是回答了關於讓oauth2在android上運行(對於任何谷歌API)的類似問題。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM