Google Calendar API with Java - 服务帐户

时间:2017-12-05 12:49:49

标签: java google-calendar-api

引用了Google Calendar API with PHP – Service Account并尝试了以下步骤:

  • 在Google API控制台中创建了一个项目,并在此项目中启用了Google Calendar API
  • 在项目下创建了一个服务帐户,下载了JWT格式密钥
  • 尝试与服务帐户电子邮件分享我的个人日历,不确定是否存在UI错误,没有错误,刷新页面后,服务帐户电子邮件消失了。我不得不使用我公司的Google帐户日历,并且可以添加服务帐户。
  • 编写Java代码以使用步骤2中的JWT键来获取日历列表。
HttpTransport httpTransport = GoogleNetHttpTransport.newTrustedTransport();
JsonFactory jsonFactory = JacksonFactory.getDefaultInstance();
// Build service account credential.

GoogleCredential credential = GoogleCredential          .fromStream(this.getClass().getClassLoader().getResourceAsStream("makoto-c14c149ef994.json")).createScoped(Collections.singleton(CalendarScopes.CALENDAR));

Calendar calendar = new Calendar.Builder(httpTransport, jsonFactory, credential).setApplicationName("makoto").build();

return calendar.calendarList().list();

问题/疑问是:

  1. 中没有列出任何内容
      

    calendar.calendarList()。表()

    返回[],也没有错误。我错过了什么吗?

    1. 为什么我不能与服务帐户共享我的个人日历。这只适用于企业版Google帐户吗?

2 个答案:

答案 0 :(得分:2)

对于#1,请尝试在SO post

中执行此处的建议

无论使用何种语言,请尝试与服务帐户共享现有日历。

<paste-your-account-here>@developer.gserviceaccount.com

要了解#2,请尝试按照此SO post中的接受答案进行操作。

  

转到Google日历网站添加服务帐户电子邮件   作为日历的用户地址。然后服务帐户将是   能够访问它。如果您愿意,可以通过编程方式将其插入   calendar.list如果你真的需要它在那里。

答案 1 :(得分:-1)

请尝试

return calendar.calendarList().list().execute();
相关问题