如何在Eclipse中使用Google Calendar API jar文件创建活动?

时间:2016-12-29 23:28:05

标签: java eclipse google-calendar-api

我在this tutorial之后遇到了困难。我在 Eclipse IDE 中编写简单的Java类,能够将新事件插入到我的Google日历中。

我已经下载了Calendar API v3 Client Library for Java,并为我的项目添加了构建路径。此外,我还为Google Calendar API创建了凭据,并下载了 client_secret.json 文件。

我正在使用:

import com.google.api.client.util.DateTime;
import com.google.api.services.calendar.model.Event;
import com.google.api.services.calendar.model.EventAttendee;
import com.google.api.services.calendar.model.EventDateTime;
import com.google.api.services.calendar.model.EventReminder;

public class CreateEventCalendar {
    // Refer to the Java quickstart on how to setup the environment:
    // https://developers.google.com/google-apps/calendar/quickstart/java
    // Change the scope to CalendarScopes.CALENDAR and delete any stored
    // credentials.

    Event event = new Event().setSummary("Google I/O 2015").setLocation("800 Howard St., San Francisco, CA 94103")
            .setDescription("A chance to hear more about Google's developer products.");

    DateTime startDateTime = new DateTime("2015-05-28T09:00:00-07:00");
    EventDateTime start = new EventDateTime().setDateTime(startDateTime)
            .setTimeZone("America/Los_Angeles");event.setStart(start);

    DateTime endDateTime = new DateTime("2015-05-28T17:00:00-07:00");
    EventDateTime end = new EventDateTime().setDateTime(endDateTime)
            .setTimeZone("America/Los_Angeles");event.setEnd(end);

    String[] recurrence = new String[] { "RRULE:FREQ=DAILY;COUNT=2" };event.setRecurrence(Arrays.asList(recurrence));

    EventAttendee[] attendees = new EventAttendee[] { new EventAttendee().setEmail("lpage@example.com"),
            new EventAttendee().setEmail("sbrin@example.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());
}}

如何在Google日历中插入新活动?我该如何授权申请?如何使用创建的凭据?

非常感谢任何帮助。

更新:

Jan 04, 2017 2:04:54 AM com.google.api.client.util.store.FileDataStoreFactory setPermissionsToOwnerOnly
WARNING: unable to change permissions for everybody: C:\Users\name\.credentials\calendar-java-quickstart
Jan 04, 2017 2:04:56 AM com.google.api.client.util.store.FileDataStoreFactory setPermissionsToOwnerOnly
WARNING: unable to change permissions for owner: C:\Users\name\.credentials\calendar-java-quickstart
Exception in thread "main" java.lang.NoClassDefFoundError: javax/servlet/http/HttpServletRequest
    at com.google.api.client.extensions.jetty.auth.oauth2.LocalServerReceiver.getRedirectUri(LocalServerReceiver.java:98)
    at com.google.api.client.extensions.java6.auth.oauth2.AuthorizationCodeInstalledApp.authorize(AuthorizationCodeInstalledApp.java:76)
    at Quickstart.authorize(Quickstart.java:79)
    at Quickstart.getCalendarService(Quickstart.java:92)
    at Quickstart.main(Quickstart.java:104)
Caused by: java.lang.ClassNotFoundException: javax.servlet.http.HttpServletRequest
    at java.net.URLClassLoader.findClass(Unknown Source)
    at java.lang.ClassLoader.loadClass(Unknown Source)
    at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
    at java.lang.ClassLoader.loadClass(Unknown Source)
    ... 5 more

我在其他网站上找到了可能的解决方案,但没有一个能够解决问题:

  1. 以管理模式运行eclipse
  2. InputStream in DriveQuickstart.class.getResourceAsStream("/client_secret.json");更改为InputStream in = new FileInputStream("<FullPath>\\client_secret.json");
  3. 当我启动快速入门课程时,&#34; StoredCredentials&#34;文件已创建。

0 个答案:

没有答案