调用Calendar Api返回403状态且使用限制超出消息 [英] Calling Calendar Api returning 403 status with use limit exceed message

查看:66
本文介绍了调用Calendar Api返回403状态且使用限制超出消息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用Java从服务器创建事件.

I am trying to create event from server using java.

这是我的相同代码.

private static final String APPLICATION_NAME = "Google Calendar API Java Quickstart";
private static final JsonFactory JSON_FACTORY = JacksonFactory.getDefaultInstance();
private static final String TOKENS_DIRECTORY_PATH = "tokens";

private static final List<String> SCOPES = Collections.singletonList(CalendarScopes.CALENDAR);
private static final String CREDENTIALS_FILE_PATH = "/credentials.json";

private static final String projfilepath = "/quickstart-foxmatrix.json";

private static Credential getCredentials(final NetHttpTransport HTTP_TRANSPORT) throws Exception {
    // Load client secrets.

    InputStream in = CalendarQuickstart.class.getResourceAsStream(CREDENTIALS_FILE_PATH);
    if (in == null) {
        throw new FileNotFoundException("Resource not found: " + CREDENTIALS_FILE_PATH);
    }

    GoogleClientSecrets clientSecrets = GoogleClientSecrets.load(JSON_FACTORY, new InputStreamReader(in));

    // Build flow and trigger user authorization request.
    GoogleAuthorizationCodeFlow flow = new GoogleAuthorizationCodeFlow.Builder(HTTP_TRANSPORT, JSON_FACTORY,
            clientSecrets, SCOPES)
                    .setDataStoreFactory(new FileDataStoreFactory(new java.io.File(TOKENS_DIRECTORY_PATH)))
                    .setAccessType("offline").build();
    LocalServerReceiver receiver = new LocalServerReceiver.Builder().setPort(9000).build();
    return new AuthorizationCodeInstalledApp(flow, receiver).authorize("user");
}

public static void main(String... args) throws Exception {
    // Build a new authorized API client service.
    final NetHttpTransport HTTP_TRANSPORT = GoogleNetHttpTransport.newTrustedTransport();

    InputStream in = CalendarQuickstart.class.getResourceAsStream(projfilepath);

    GoogleCredential credential = GoogleCredential.fromStream(in)
            .createScoped(Collections.singleton(CalendarScopes.CALENDAR));

    Calendar service = new Calendar.Builder(HTTP_TRANSPORT, JSON_FACTORY, credential)
            .setApplicationName(APPLICATION_NAME).build();
    CalendarQuickstart obj = new CalendarQuickstart();
    obj.createEvent(service);
}

public String createEvent(Calendar service) throws IOException {
    Event event = new Event().setSummary("New Event")
            .setDescription("A chance to hear more about Google's developer products.");

    DateTime startDateTime = new DateTime("2020-02-04T09:00:00-07:00");
    EventDateTime start = new EventDateTime().setDateTime(startDateTime);
    event.setStart(start);

    DateTime endDateTime = new DateTime("2020-02-04T17:00:00-07:00");
    EventDateTime end = new EventDateTime().setDateTime(endDateTime);
    event.setEnd(end);

    EventAttendee[] attendees = new EventAttendee[] {
            new EventAttendee().setEmail("hanil.kathuria@innovationm.com"),
            new EventAttendee().setEmail("jijo.mathew@innovationm.com"), };
    event.setAttendees(Arrays.asList(attendees));

    EventReminder[] reminderOverrides = new EventReminder[] {
            new EventReminder().setMethod("email").setMinutes(24 * 60),
            new EventReminder().setMethod("popup").setMinutes(10), };
    Event.Reminders reminders = new Event.Reminders().setUseDefault(false)
            .setOverrides(Arrays.asList(reminderOverrides));
    event.setReminders(reminders);

    String calendarId = "primary";
    event = service.events().insert(calendarId, event).execute();
    System.out.printf("Event created: %s\n", event.getHtmlLink());
    event.getHangoutLink();
    return event.getHtmlLink();
}

我正在使用json文件中的服务帐户凭据.

i am using my service account credentials from json file.

我正在使用Google客户端库进行api调用.

I am using google client library for api calling.

我还检查了我的仪表板,并且api调用的限制为100/秒.并且没有针对api调用显示流量.仍然,当我尝试创建事件时,它显示超出使用限制.

I have also check my dashboard and the limit for api call is 100/sec. and there is no trafic shown for the api call. Still when i am trying to create an event it is showing use limit exceeded.

线程"main"中的异常com.google.api.client.googleapis.json.GoogleJsonResponseException:403禁止{代码":403,错误":[{域":"usageLimits",消息":,"超出了日历使用限制.,"原因:" quotaExceeded}],"消息:"超过了日历使用限制."}

Exception in thread "main" com.google.api.client.googleapis.json.GoogleJsonResponseException: 403 Forbidden { "code" : 403, "errors" : [ { "domain" : "usageLimits", "message" : "Calendar usage limits exceeded.", "reason" : "quotaExceeded" } ], "message" : "Calendar usage limits exceeded." }

推荐答案

您收到此错误,是因为您试图在不使用帐户模拟的情况下与参与者创建事件,并且目前无法做到这一点>,如您在此问题在Google Issue Tracker中看到的.

You are getting this error because you are trying to create an event with attendees without using account impersonation, and doing this is currently not possible, as you can see in this issue in Google Issue Tracker.

如果可能的话,您可以先与服务帐户共享日历,然后添加活动.但这是不可能的,所以您应该使用服务帐户来模拟自己:它应该代表您添加事件 .

If this was possible, you could first share your calendar with the Service Account and then add the event. But because this is not possible, you should use the Service Account to impersonate yourself: it should add the event on behalf of you.

(重要提示:如果您使用服务帐户没有模拟行为,并将 calendarId 设置为 primary ,就像您共享的代码一样,您不会将事件添加到主日历中,而是会添加到服务帐户的主日历中.服务帐户具有自己的主日历,自己的云端硬盘等,不是与您的主日历,云端硬盘等相同).

(Important note: if you use a Service Account without impersonation and set the calendarId to primary, as in the code you shared, you won't add the event to your primary calendar, but to the Service Account's primary calendar. A Service Account has its own primary Calendar, its own Drive, etc. which is not the same as your primary Calendar, your Drive, etc.).

如果要使用服务帐户代表您访问资源,则必须执行以下操作:

If you want to use a Service Account to reach a resource on behalf of you, you have to do the following:

    通过遵循授予服务范围内的域范围内的授权>此处指定的步骤.将服务帐户ID作为 Client Name https://www.googleapis.com/auth/calendar 添加为 Manage API Client access 中的范围>.
  • 下载与服务帐户对应的凭据(在我在下面提供的示例中,使用P12代替JSON).
  • 使用服务帐户来模拟自己,方法是在构建凭据时指出您的电子邮件地址,如下面的示例所示.
  • Grant domain-wide delegation to the Service Account by following the steps specified here. Add the service account ID as Client Name and https://www.googleapis.com/auth/calendar as a scope in Manage API client access.
  • Download the credentials corresponding to the Service Account (in the sample I provide below, P12 is used instead of JSON).
  • Use the Service Account to impersonate yourself by indicating your email address when building the credentials, as indicated in the sample below.

您可以通过以下方式构建凭据:

You could build your credentials the following way:

GoogleCredential credential = new GoogleCredential.Builder()
    .setTransport(httpTransport)
    .setJsonFactory(JSON_FACTORY)
    .setServiceAccountId("service-account@email-address") // Service account email
    .setServiceAccountPrivateKeyFromP12File(new File("your-credentials.p12"))
    .setServiceAccountScopes(Collections.singleton(CalendarScopes.CALENDAR))
    .setServiceAccountUser("user@email-address") // Your email address (address of the user you want to impersonate)
    .build();

参考:

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆