如何将日历范围添加到谷歌云服务帐户

时间:2021-07-28 05:40:01

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

我正在使用 google api 服务访问 google calender API。 这是我的 Java 示例

GoogleCredential credentials = new GoogleCredential.Builder()
                .setTransport(GoogleNetHttpTransport.newTrustedTransport())
                .setJsonFactory(JacksonFactory.getDefaultInstance())
                .setServiceAccountId("")
                .setServiceAccountScopes(Arrays.asList("https://www.googleapis.com/auth/calendar.readonly"))
                .setServiceAccountPrivateKeyFromP12File(
                        new File(""))
                .build();
        com.google.api.services.calendar.Calendar client = new com.google.api.services.calendar.Calendar.Builder(GoogleNetHttpTransport.newTrustedTransport(),
                JacksonFactory.getDefaultInstance(), credentials).build();

        Calendar calendar = addCalendar(client);

        private static Calendar addCalendar(com.google.api.services.calendar.Calendar client) throws IOException {
        Calendar entry = new Calendar();
        entry.setSummary("Calendar for Testing 3");
        Calendar result = client.calendars().insert(entry).execute();
        return result;
    }

    Response:
    POST https://oauth2.googleapis.com/token
{
  "error" : "invalid_scope",
  "error_description" : "Invalid OAuth scope or ID token audience provided."
}

我应该从哪里将日历范围设置为服务帐户

1 个答案:

答案 0 :(得分:0)

您已经声明了只读范围。

.setServiceAccountScopes(Arrays.asList("https://www.googleapis.com/auth/calendar.readonly"))

您是否尝试过向数组添加更多内容?