【问题标题】:Google Calendar API with Java - Service Account带有 Java 的 Google Calendar API - 服务帐户
【发布时间】:2017-12-05 12:49:48
【问题描述】:

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

  • 在 Google API 控制台中创建了一个项目并在该项目上启用了 Google Calendar API
  • 在项目下创建服务账号,下载JWT格式密钥
  • 尝试将我的个人日历分享给服务帐号邮箱,不知道是UI bug还是不允许,没有错误,刷新页面后,服务帐号邮箱不见了。我必须使用我公司的 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().list()

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

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

【问题讨论】:

    标签: java google-calendar-api


    【解决方案1】:

    对于#1,请尝试在SO post 中提出建议。

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

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

    要深入了解#2,请尝试遵循SO post 中公认的答案。

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

    【讨论】:

      【解决方案2】:

      请尝试

      return calendar.calendarList().list().execute();
      

      【讨论】:

      • 虽然此代码可以回答问题,但提供有关 如何 和 为什么 解决问题的附加上下文将提高​​答案的长期价值。
      猜你喜欢
      • 2012-12-02
      • 1970-01-01
      • 2017-11-04
      • 1970-01-01
      • 1970-01-01
      • 2014-09-01
      • 1970-01-01
      • 2017-06-26
      • 2020-07-03
      相关资源
      最近更新 更多