【问题标题】:Google calendar API returns UnauthorizedGoogle 日历 API 返回未经授权
【发布时间】:2019-04-09 21:11:56
【问题描述】:

我有一个这样构建的日历客户端

public static Calendar getCalendar(Credential credentials) throws GeneralSecurityException, IOException {
        return new Calendar.Builder(GoogleNetHttpTransport.newTrustedTransport(), JSON_FACTORY, credentials)
                .setApplicationName(APPLICATION_NAME)
                .build();
    }

calendar.events().list("primary").execute() 工作并返回结果

但是这个抱怨401:未授权

  public static String subscirbeToHook(Credential user, Calendar service) throws IOException {

        Channel content = new Channel()
                .setId(UUID.randomUUID().toString())
                .setType("web_hook")
                .setResourceUri("https://www.googleapis.com/calendar/v3/calendars/primary/events/watch")
                .setAddress("https://2e8e18e3.ngrok.io/");


        Channel channel = service.events()
                .watch("primary", content)
                .execute();


        return channel.getResourceId();
    }

我请求https://www.googleapis.com/auth/calendar 范围。我迷路了。我为资源 uri 尝试了不同的组合

【问题讨论】:

    标签: google-calendar-api google-api-java-client


    【解决方案1】:

    您似乎无法对resourceURI 进行更改,因为Calendar Push Notifications 目前不支持它。

    目前,Google Calendar API 支持更改通知 到 Acl、CalendarList、Events 和 Settings 资源。

    这些是受支持的Events resources 请求正文:

    {
      "id": string,
      "token": string,
      "type": string,
      "address": string,
      "params": {
        "ttl": string
      }
    }
    

    如果成功,则此方法返回具有以下结构的响应正文:

    {
      "kind": "api#channel",
      "id": string,
      "resourceId": string,
      "resourceUri": string,
      "token": string,
      "expiration": long
    }
    

    【讨论】:

    • 我正在关注事件,这是受支持的
    • 我已经编辑了答案。事件也是如此。正文请求与设置资源相同。
    猜你喜欢
    • 1970-01-01
    • 2014-10-22
    • 2017-03-06
    • 2016-12-24
    • 1970-01-01
    • 2014-11-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多