如何使用 Java 访问 Google Calendar REST API v3
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20076618/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to Access Google Calendar REST API v3 with Java
提问by AntonH
I'm working on a project where I have to access a set of Google Calendars using REST and working with Java.
我正在开发一个项目,在该项目中我必须使用 REST 并使用 Java 访问一组 Google 日历。
The program, situated on a private non-Google server, periodically (via cron job) connects to the Google account, gets the list of Calendars linked to the account, gets the last month's events of each calendar, and returns an XML file containing all the information. The program should be able to execute and access calendars WITHOUT any user input. For the moment, the project specifies that the Calendar will just be read, not modified (so only GET/LIST calls will be made).
该程序位于私人非 Google 服务器上,定期(通过 cron 作业)连接到 Google 帐户,获取链接到该帐户的日历列表,获取每个日历上个月的事件,并返回一个 XML 文件,其中包含所有信息。该程序应该能够在没有任何用户输入的情况下执行和访问日历。目前,该项目指定只读取日历,而不修改日历(因此只会进行 GET/LIST 调用)。
I have looked through the Google documentation and been over the sample codes using client libraries, and almost all examples given require OAuth 2.0 user consent before accessing calendar API. Even the REST API documentation pages require you activate OAuth 2.0 to return requested information (otherwise returning a HTTP 40X error code and JSON file containing error status and message).
我浏览了 Google 文档并使用客户端库查看了示例代码,几乎所有给出的示例在访问日历 API 之前都需要 OAuth 2.0 用户同意。甚至 REST API 文档页面也要求您激活 OAuth 2.0 以返回请求的信息(否则返回 HTTP 40X 错误代码和包含错误状态和消息的 JSON 文件)。
How could I connect to the Google Calendar REST API to get the information I need, everything being done via REST calls, and without requiring user consent at the time of execution?
我怎样才能连接到 Google Calendar REST API 来获取我需要的信息,一切都是通过 REST 调用完成的,并且在执行时不需要用户同意?
Or am I over-complicating things, and only require the 'Server Key', found in the Resgistered Apps part of the Google Cloud Console?
Or am I requiring to use both OAuth and developer key? (I found someone mention it here under a question with the title : Google Calendar API v3 hardcoded credentials ; however, the question and solution were for PHP, and I don't know if something similar would be possible or necessary in Java).
或者我是否过于复杂,只需要在 Google Cloud Console 的注册应用程序部分中找到的“服务器密钥”?
还是我需要同时使用 OAuth 和开发人员密钥?(我发现有人在这里提到了一个标题为:Google Calendar API v3 hardcoded credentials 的问题;但是,问题和解决方案是针对 PHP 的,我不知道在 Java 中是否可能或需要类似的东西)。
- I did see the possibility of creating a JWT with a service account (with Google cloud console, register as a Web Application, and it's under 'certificate'), but I did not find how I would use this in conjunction with a REST call from a Java program.
- The following link (http://aleksz-programming.blogspot.be/2012/11/google-calendar-api.html) says that it is possible to access information via 'Web server applications' or 'Installed applications', but doesn't go into detail. However, on the Google OAuth 2.0 documentation page, the 'Web server applications' part still requires user input & consent. Which is true?
- I also found this page (https://developers.google.com/accounts/docs/OAuth2Login), but failed to see how it could be used without using a user consent page.
- I did see some references to a '2-legged OAuth'. But it seems that this is not a OAuth 2.0, but a version 1 was of doing things. Is it a possible solution for my project? If so, what information would I need from Google Cloud Console to get it working?
- 我确实看到了使用服务帐户创建 JWT 的可能性(使用 Google 云控制台,注册为 Web 应用程序,并且它在“证书”下),但我没有找到如何将它与来自的 REST 调用结合使用一个Java程序。
- 以下链接 ( http://aleksz-programming.blogspot.be/2012/11/google-calendar-api.html) 表示可以通过“网络服务器应用程序”或“已安装的应用程序”访问信息,但不能不细说。但是,在 Google OAuth 2.0 文档页面上,“Web 服务器应用程序”部分仍然需要用户输入和同意。哪个是真的?
- 我也找到了这个页面(https://developers.google.com/accounts/docs/OAuth2Login),但没有看到如何在不使用用户同意页面的情况下使用它。
- 我确实看到了一些对“2-legged OAuth”的引用。但似乎这不是一个 OAuth 2.0,而是一个版本 1 的服务器。这是我的项目的可能解决方案吗?如果是这样,我需要从 Google Cloud Console 获取哪些信息才能使其正常工作?
I hope I have given enough information as to what I'm looking for, and also that this question hasn't been covered so far. I did research it, and didn't find anything close enough to what I was looking for.
我希望我已经提供了关于我正在寻找的内容的足够信息,并且到目前为止还没有涉及到这个问题。我确实研究过它,但没有找到与我想要的东西足够接近的东西。
I was unable to post more than 2 links (lacking reputation), but did look at the Google Calendar v3 REST API, as well as the page specifying the creation of a JWT.
我无法发布超过 2 个链接(缺乏声誉),但确实查看了 Google Calendar v3 REST API,以及指定创建 JWT 的页面。
采纳答案by aeijdenberg
If you just need to access a particular set of calendars, I would create a service account and share the necessary calendars with that account.
如果您只需要访问一组特定的日历,我会创建一个服务帐户并与该帐户共享必要的日历。
To do so:
这样做:
- Create a "Service Account" in this Cloud Console (it is found under "Web application" / "Certificate").
- Download the private key and store in a safe place.
- Take note of the email address associated with the service account.
- Share (via Calendar user interface) any necessary calendars with this email address.
- Install Google API Java Client libraries (https://developers.google.com/api-client-library/java/apis/).
- 在此 Cloud Console 中创建一个“服务帐户”(位于“Web 应用程序”/“证书”下)。
- 下载私钥并将其存储在安全的地方。
- 记下与服务帐户关联的电子邮件地址。
- 与此电子邮件地址共享(通过日历用户界面)任何必要的日历。
- 安装 Google API Java 客户端库 ( https://developers.google.com/api-client-library/java/apis/)。
Then you should be able to use the following code:
那么你应该可以使用以下代码:
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();
If instead you are a domain administrator who needs to access calendars for all Google Apps accounts that are part of your domain without consent from individual users, then instead of step 4 above:
相反,如果您是域管理员,需要在未经个人用户同意的情况下访问属于您域的所有 Google Apps 帐户的日历,那么请不要执行上述第 4 步:
- Take note of the client ID associated with the service account. This can be found in the client_secrets.json file - typically in the form 1234.apps.googleusercontent.com.
- Authorize this client to make requests on behalf of users in your organization. See https://support.google.com/a/answer/162106?hl=enfor steps - use whichever scopes you will be requesting later.
- 记下与服务帐户关联的客户端 ID。这可以在 client_secrets.json 文件中找到 - 通常采用 1234.apps.googleusercontent.com 的形式。
- 授权此客户端代表您组织中的用户提出请求。有关步骤,请参阅https://support.google.com/a/answer/162106?hl=en- 使用您稍后将请求的任何范围。
You should now be able to write code like the following:
您现在应该能够编写如下代码:
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>()