【问题标题】:Cannot impersonate service account to create calendar events无法模拟服务帐户来创建日历事件
【发布时间】:2021-08-12 18:51:00
【问题描述】:

我正在尝试使用 google 服务帐户实现用户模拟,并且已经有一段时间出现问题,这是我在 Java 中使用的代码:

GoogleCredential credential = GoogleCredential
        .fromStream(TestGoogleCalendarEventCreate.class
            .getResourceAsStream("/ph3-rovigo-313910-6094af96ccfc.json"))
        .createScoped(CalendarScopes.all()).createDelegated("no-reply@dutytree.it");

当我尝试从共享日历中读取事件时,我收到以下身份验证错误:

com.google.api.client.auth.oauth2.TokenResponseException: 401 Unauthorized 发布https://oauth2.googleapis.com/token

如果我不使用模拟,我可以阅读活动,但不能邀请与会者参加新创建的活动。 服务帐户在 G-Suite 域中启用了域范围的权限 如以下屏幕截图所示。客户端 ID 是服务帐户用户的客户端 ID。我是否需要向任何用户授予其他权限?

非常感谢。

【问题讨论】:

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


    【解决方案1】:

    我认为你应该使用setServiceAccountUser

     GoogleCredential credential = new GoogleCredential.Builder()
          .setTransport(httpTransport)
          .setJsonFactory(jsonFactory)
          .setServiceAccountId(SERVICE_ACCOUNT_EMAIL)
          .setServiceAccountScopes(DirectoryScopes.ADMIN_DIRECTORY_USERS)
          .setServiceAccountUser(userEmail)
          .setServiceAccountPrivateKeyFromP12File(
              new java.io.File(SERVICE_ACCOUNT_PKCS12_FILE_PATH))
          .build();
    

    【讨论】:

    • 我也尝试过这种方式,使用 CalendarScopes.all(),但没有运气。在身份验证阶段总是收到错误 401。
    • 我认为你应该检查一下你的代表团。
    【解决方案2】:

    在我的情况下,以下作品:

    确保将域范围的权限委派给createScoped(...) 中请求的所有范围。

    在上述情况下,您使用的 .createScoped(CalendarScopes.all()) 包含您未提供权限的 calendar.settings.readonly

    替换为:

    .createScoped(ImmutableList.of(CalendarScopes.CALENDAR, CalendarScopes.CALENDAR_EVENTS))
    .createDelegated("no-reply@dutytree.it")
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-10-31
      • 1970-01-01
      • 1970-01-01
      • 2018-02-10
      • 2012-08-05
      • 2022-07-07
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多