如何使用 Java 访问 Google Calendar REST API v3 [英] How to Access Google Calendar REST API v3 with Java

查看:27
本文介绍了如何使用 Java 访问 Google Calendar REST API v3的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在处理一个项目,我必须使用 REST 和 Java 访问一组 Google 日历.

I'm working on a project where I have to access a set of Google Calendars using REST and working with Java.

该程序位于私人非 Google 服务器上,定期(通过 cron 作业)连接到 Google 帐户,获取链接到该帐户的日历列表,获取每个日历上个月的事件,并返回一个 XML包含所有信息的文件.该程序应该能够在没有任何用户输入的情况下执行和访问日历.目前,该项目指定仅读取日历,而不修改日历(因此只会进行 GET/LIST 调用).

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 文档并使用了客户端库查看了示例代码,并且几乎所有给出的示例都需要 OAuth 2.0 用户同意才能访问日历 API.甚至 REST API 文档页面也要求您激活 OAuth 2.0 以返回请求的信息(否则返回 HTTP 40X 错误代码和包含错误状态和消息的 JSON 文件).

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 Calendar REST API 来获取我需要的信息,一切都是通过 REST 调用完成的,并且在执行时不需要用户同意?

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 Cloud Console 的注册应用程序部分中找到的服务器密钥"?
还是我需要同时使用 OAuth 和开发人员密钥?(我发现有人在这里提到了一个标题为:Google Calendar API v3 hardcoded credentials 的问题;但是,问题和解决方案是针对 PHP 的,我不知道在 Java 中是否可能或需要类似的东西).

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).

  • 我确实看到了使用服务帐户创建 JWT 的可能性(使用 Google 云控制台,注册为 Web 应用程序,并且它在证书"下),但我没有找到如何将其与来自 Java 程序的 REST 调用.
  • 以下链接(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 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?

我希望我已经提供了关于我正在寻找的内容的足够信息,并且到目前为止还没有涉及到这个问题.我确实研究了它,但没有找到任何与我想要的东西足够接近的东西.

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.

我无法发布超过 2 个链接(缺乏声誉),但确实查看了 Google Calendar v3 REST API,以及指定创建 JWT 的页面.

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.

推荐答案

如果您只需要访问一组特定的日历,我会创建一个服务帐户并与该帐户共享必要的日历.

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.

这样做:

  1. 在此 Cloud Console 中创建服务帐户"(位于Web 应用程序"/证书"下).
  2. 下载私钥并保存在安全的地方.
  3. 记下与服务帐户关联的电子邮件地址.
  4. 与此电子邮件地址共享(通过日历用户界面)任何必要的日历.
  5. 安装 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();

如果您是域管理员,需要在未经个人用户同意的情况下访问属于您域的所有 Google Apps 帐户的日历,则不要执行上述第 4 步:

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:

  1. 记下与服务帐户关联的客户端 ID.这可以在 client_secrets.json 文件中找到 - 通常采用 1234.apps.googleusercontent.com 的形式.
  2. 授权此客户端代表您组织中的用户提出请求.请参阅https://support.google.com/a/answer/162106?hl=en对于步骤 - 使用您稍后将请求的任何范围.
  1. 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.
  2. Authorize this client to make requests on behalf of users in your organization. See https://support.google.com/a/answer/162106?hl=en for steps - use whichever scopes you will be requesting later.

您现在应该能够编写如下代码:

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>()

这篇关于如何使用 Java 访问 Google Calendar REST API v3的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆