我正在尝试使用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("[email protected]"),
new EventAttendee().setEmail("[email protected]"), };
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文件中的服务帐户凭据。
我正在使用Google客户端库进行api调用。
我还检查了我的仪表板,并且api调用的限制为100 /秒。并且没有针对api调用显示流量。当我尝试创建事件时,它仍然显示超出使用限制。
线程“ main” com.google.api.client.googleapis.json.GoogleJsonResponseException中的异常:403禁止{“代码”:403,“错误”:[{“域”:“ usageLimits”,“消息”:“超出日历使用限制。“,”原因“:” quotaExceeded“}],”消息“:”超出日历使用限制。“ }
您使用的不是针对服务帐户的Oauth2代码,Google Analytics(分析)API的Java服务帐户教程非常好,适用于Java Quickstart service account java,应该不难将其替换为日历。
private static Calendar initializeCalendar() throws GeneralSecurityException, IOException {
HttpTransport httpTransport = GoogleNetHttpTransport.newTrustedTransport();
GoogleCredential credential = GoogleCredential
.fromStream(new FileInputStream(KEY_FILE_LOCATION))
.createScoped(CalendarScopes.CALENDAR));
// Construct the Analytics service object.
return new Calendar.Builder(httpTransport, JSON_FACTORY, credential)
.setApplicationName(APPLICATION_NAME).build();
}
public static void main(String... args) throws IOException, GeneralSecurityException {
// Build a new authorized API client service.
Calendar service = initializeCalendar();
// List the next 10 events from the primary calendar.
DateTime now = new DateTime(System.currentTimeMillis());
Events events = service.events().list("primary")
.setMaxResults(10)
.setTimeMin(now)
.setOrderBy("startTime")
.setSingleEvents(true)
.execute();
List<Event> items = events.getItems();
if (items.isEmpty()) {
System.out.println("No upcoming events found.");
} else {
System.out.println("Upcoming events");
for (Event event : items) {
DateTime start = event.getStart().getDateTime();
if (start == null) {
start = event.getStart().getDate();
}
System.out.printf("%s (%s)\n", event.getSummary(), start);
}
}
这应该很近,但是我现在没有Java编译器的功能。
您收到此错误,是因为您试图与与会者创建事件而不使用帐户模拟,并且执行此操作[[当前无法使用,如this issue in Google Issue Tracker中所示。
如果可能的话,您可以先与服务帐户共享日历,然后添加活动。但这是不可能的,因此您应该使用服务帐户来模仿自己:它应该添加事件代表
您。(重要说明
:如果您使用服务帐户无模拟用户并将calendarId
设置为primary
,如您共享的代码中一样,则不会将事件添加到主帐户中日历,但指向服务帐户的主日历。服务帐户具有自己的主日历,自己的驱动器等,与主日历,驱动器等not相同。工作流程:Client Name
中将服务帐户ID添加为https://www.googleapis.com/auth/calendar
,并将Manage API client access
作为作用域。下载与服务帐户相对应的凭据(在我在下面提供的示例中,使用P12代替JSON)。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();
参考: