[英]How to Access Google Calendar REST API v3 with Java
我正在開發一個項目,在該項目中我必須使用 REST 並使用 Java 訪問一組 Google 日歷。
該程序位於私人非 Google 服務器上,定期(通過 cron 作業)連接到 Google 帳戶,獲取鏈接到該帳戶的日歷列表,獲取每個日歷上個月的事件,並返回一個包含所有內容的 XML 文件信息。 該程序應該能夠在沒有任何用戶輸入的情況下執行和訪問日歷。 目前,該項目指定只讀取日歷,而不修改日歷(因此只會進行 GET/LIST 調用)。
我瀏覽了 Google 文檔並使用客戶端庫查看了示例代碼,幾乎所有給出的示例在訪問日歷 API 之前都需要 OAuth 2.0 用戶同意。 甚至 REST API 文檔頁面也要求您激活 OAuth 2.0 以返回請求的信息(否則返回 HTTP 40X 錯誤代碼和包含錯誤狀態和消息的 JSON 文件)。
我怎樣才能連接到 Google Calendar REST API 來獲取我需要的信息,一切都是通過 REST 調用完成的,並且在執行時不需要用戶同意?
或者我是否過於復雜,只需要在 Google Cloud Console 的注冊應用程序部分中找到的“服務器密鑰”?
還是我需要同時使用 OAuth 和開發人員密鑰? (我發現有人在這里提到了一個標題為:Google Calendar API v3 hardcoded credentials 的問題;但是,問題和解決方案是針對 PHP 的,我不知道在 Java 中是否可能或需要類似的東西)。
我希望我已經提供了關於我正在尋找的內容的足夠信息,並且到目前為止還沒有涉及到這個問題。 我確實研究過它,但沒有找到與我想要的東西足夠接近的東西。
我無法發布超過 2 個鏈接(缺乏聲譽),但確實查看了 Google Calendar v3 REST API,以及指定創建 JWT 的頁面。
如果您只需要訪問一組特定的日歷,我會創建一個服務帳戶並與該帳戶共享必要的日歷。
這樣做:
那么你應該可以使用以下代碼:
import com.google.api.client.googleapis.auth.oauth2.GoogleCredential;
import com.google.api.client.json.gson.GsonFactory;
import java.io.File;
import com.google.api.client.googleapis.javanet.GoogleNetHttpTransport;
import java.util.Arrays;
import com.google.api.services.calendar.Calendar;
GoogleCredential credentials = new GoogleCredential.Builder().setTransport(GoogleNetHttpTransport.newTrustedTransport())
.setJsonFactory(new GsonFactory())
.setServiceAccountId("<service account email address>@developer.gserviceaccount.com")
.setServiceAccountScopes(Arrays.asList("https://www.googleapis.com/auth/calendar.readonly"))
.setServiceAccountPrivateKeyFromP12File(new File("<private key for service account in P12 format>-privatekey.p12"))
.build();
Calendar client = new Calendar.Builder(GoogleNetHttpTransport.newTrustedTransport(), new GsonFactory(), credentials).build();
client.<do calendar stuff>.execute();
相反,如果您是域管理員,需要在未經個人用戶同意的情況下訪問屬於您域的所有 Google Apps 帳戶的日歷,那么請不要執行上述第 4 步:
您現在應該能夠編寫如下代碼:
import com.google.api.client.googleapis.auth.oauth2.GoogleCredential;
import com.google.api.client.json.gson.GsonFactory;
import java.io.File;
import com.google.api.client.googleapis.javanet.GoogleNetHttpTransport;
import java.util.Arrays;
import com.google.api.services.calendar.Calendar;
GoogleCredential credentials = new GoogleCredential.Builder().setTransport(GoogleNetHttpTransport.newTrustedTransport())
.setJsonFactory(new GsonFactory())
.setServiceAccountId("<service account email address>@developer.gserviceaccount.com")
.setServiceAccountScopes(Arrays.asList("https://www.googleapis.com/auth/calendar"))
.setServiceAccountPrivateKeyFromP12File(new File("<private key for service account in P12 format>-privatekey.p12"))
.setServiceAccountUser("<domain user whose data you need>@yourdomain.com")
.build();
Calendar client = new Calendar.Builder(GoogleNetHttpTransport.newTrustedTransport(), new GsonFactory(), credentials).build();
client.<do calendar stuff as that user>()
除了@aeijdenberg 的回答中提到的步驟之外,服務帳戶現在需要通過 CalendarList.insert 將共享日歷添加到其 CalendarList 來明確接受共享日歷。 見:
服務帳戶不再接受自動共享日歷 [148804709] https://issuetracker.google.com/issues/148804709
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.