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

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

问题描述

我正在开发一个项目,在这个项目中,我必须使用REST并使用Java来访问一组Google Calendar。



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



我浏览过Google文档,并使用客户端库对示例代码进行了说明,并且在访问日历API之前,几乎所有示例都要求OAuth 2.0用户同意。即使是REST API文档页面也要求您激活OAuth 2.0以返回请求的信息(否则返回HTTP 40X错误代码和包含错误状态和消息的JSON文件)。

我连接到Google日历REST API以获取我需要的信息,所有事情都是通过REST调用完成的,并且在执行时无需用户同意?

或者我过于复杂的事情,只需要在Google云端控制台的Resgistered Apps部分中找到的'Server Key'?
或者我需要同时使用OAuth和开发人员密钥? (我发现有人在标题为Google Calendar API v3硬编码证书的问题下提到它;但是,问题和解决方案是针对PHP的,我不知道Java中是否有类似的东西可能或必需)。 / p>


  • 我确实看到使用服务帐户创建JWT的可能性(使用Google云端控制台,注册为Web应用程序,证书'),但是我没有找到如何将它与Java程序中的REST调用结合使用。

  • 以下链接( http://aleksz-programming.blogspot.be/2012/11/google-calendar-api。 HTML )表示可以通过Web服务器应用程序或已安装的应用程序访问信息,但没有详细说明。但是,在Google OAuth 2.0文档页面上,'Web服务器应用程序'部分仍然需要用户输入&同意。这是真的吗?

  • 我也发现这个网页( https://developers.google.com/accounts/docs/OAuth2Login ),但没有看到如何在不使用用户许可页面的情况下使用它。

  • <李>我确实看到了一些对双腿OAuth的引用。但似乎这不是一个OAuth 2.0,但版本1是做事情的。这是我的项目可能的解决方案吗?如果是这样,我需要从Google Cloud Console获得哪些信息才能使其运行?


我希望我已经提供了足够的信息我正在寻找,而且这个问题迄今尚未涉及。我做了研究,没有找到足够接近我想要的东西。



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

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



为此:


  1. 在此云端控制台中创建一个服务帐户(可在Web应用程序/证书下找到)。

  2. 下载私钥并存储在安全的地方。

  3. 记下与服务帐户关联的电子邮件地址。

  4. 用户界面)包含此电子邮件地址的所有必需日历。

  5. 安装Google API Java客户端库( HT tps://developers.google.com/api-client-library/java/apis/ )。

然后您应该可以使用以下代码:

  import com.google.api.client.googleapis.auth.oauth2.GoogleCredential ; 
import com.google.api.client.json.gson.GsonFactory;
import java.io.File;
导入com.google.api.client.googleapis.javanet.GoogleNetHttpTransport;
import java.util.Arrays;
导入com.google.api.services.calendar.Calendar;

GoogleCredential credentials = new GoogleCredential.Builder()。setTransport(GoogleNetHttpTransport.newTrustedTransport())
.setJsonFactory(new GsonFactory())
.setServiceAccountId(< service account电子邮件地址> @ developer.gserviceaccount.com)
.setServiceAccountScopes(Arrays.asList(https://www.googleapis.com/auth/calendar.readonly))
.setServiceAccountPrivateKeyFromP12File(new File (< P12格式的服务帐户的私钥> -privatekey.p12))
.build();
Calendar client = new Calendar.Builder(GoogleNetHttpTransport.newTrustedTransport(),new GsonFactory(),credentials).build();
client。< do calendar stuff> .execute();

如果您是域管理员,需要访问所有Google Apps帐户的日历您的域名未经个人用户同意,那么不要使用上述第4步:


  1. 记下与服务帐户关联的客户端ID。这可以在client_secrets.json文件中找到 - 通常以1234.apps.googleusercontent.com形式。

  2. 授权此客户端代表组织中的用户发出请求。请参阅 https://support.google.com/a/answer/162106?hl=zh_CN 步骤 - 使用您稍后请求的任何范围。

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

  import com.google.api.client.googleapis.auth.oauth2.GoogleCredential; 
import com.google.api.client.json.gson.GsonFactory;
import java.io.File;
导入com.google.api.client.googleapis.javanet.GoogleNetHttpTransport;
import java.util.Arrays;
导入com.google.api.services.calendar.Calendar;

GoogleCredential credentials = new GoogleCredential.Builder()。setTransport(GoogleNetHttpTransport.newTrustedTransport())
.setJsonFactory(new GsonFactory())
.setServiceAccountId(< service account电子邮件地址> @ developer.gserviceaccount.com)
.setServiceAccountScopes(Arrays.asList(https://www.googleapis.com/auth/calendar))
.setServiceAccountPrivateKeyFromP12File(new File( < P12格式的服务帐户专用密钥> -privatekey.p12))
.setServiceAccountUser(<您需要其数据的域用户> @ yourdomain.com)
.build();
Calendar client = new Calendar.Builder(GoogleNetHttpTransport.newTrustedTransport(),new GsonFactory(),credentials).build();
client。<以该用户的身份执行日历事务>()


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

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

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

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?

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

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

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.

To do so:

  1. Create a "Service Account" in this Cloud Console (it is found under "Web application" / "Certificate").
  2. Download the private key and store in a safe place.
  3. Take note of the email address associated with the service account.
  4. Share (via Calendar user interface) any necessary calendars with this email address.
  5. Install Google API Java Client libraries (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:

  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日历REST API v3的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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