程序问答   发布时间:2022-06-02  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了如何使用Java访问Google Calendar REST API v3大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。

如何解决如何使用Java访问Google Calendar REST API v3?

开发过程中遇到如何使用Java访问Google Calendar REST API v3的问题如何解决?下面主要结合日常开发的经验,给出你关于如何使用Java访问Google Calendar REST API v3的解决方法建议,希望对你解决如何使用Java访问Google Calendar REST API v3有所启发或帮助;

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

为此:

  1. 在此Cloud Console中创建一个“服务帐户”(可在“ Web应用程序” /“证书”下找到)。
  2. 下载私钥并将其存储在安全的地方。
  3. 记下与服务帐户关联的电子邮件地址。
  4. 与该电子邮件地址共享(通过日历用户界面)任何必要的日历。
  5. 安装Google Api Java客户端库(https://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;
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:

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

解决方法

我正在一个项目中,我必须使用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的Resgistered Apps部分中找到的“服务器密钥”?
还是我需要同时使用OAuth和开发人员密钥?(我在这里发现有人在标题为“ Google Calendar API
v3硬编码凭据”的问题下提到它;但是,该问题和解决方案是针对php的,我不知道在Java中是否有可能或类似的事情)。

  • 我确实看到了使用服务帐户(通过Google云控制台,注册为Web应用程序,并且在“证书”下)创建JWT的可能性,但是我没有发现如何将其与REST调用结合使用一个Java程序。

  • 以下链接(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获得什么信息才能使其正常工作?

我希望我已经找到了足够的信息以寻找所需的信息,并且到目前为止,这个问题还没有涉及到。我确实对其进行了研究,但没有找到与我要找的东西足够接近的东西。

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

大佬总结

以上是大佬教程为你收集整理的如何使用Java访问Google Calendar REST API v3全部内容,希望文章能够帮你解决如何使用Java访问Google Calendar REST API v3所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。