java和google api日历的问题

时间:2018-03-13 20:52:37

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

我正在使用java和google api日历,但是当我尝试使用此代码创建对象日历时:

Calendar calendar = new Calendar.Builder(HTTP_TRANSPORT,JSON_FACTORY, credential).build();

控制台告诉我下一个例外:

Exception in thread "main" java.lang.NoSuchMethodError: com.google.api.client.googleapis.services.json.AbstractGoogleJsonClient.<init>(Lcom/google/api/client/http/HttpTransport;Lcom/google/api/client/http/HttpRequestInitializer;Ljava/lang/String;Ljava/lang/String;Lcom/google/api/client/json/JsonObjectParser;Lcom/google/api/client/googleapis/services/GoogleClientRequestInitializer;Ljava/lang/String;Z)V
    at com.google.api.services.calendar.Calendar.<init>(Calendar.java:135)
    at com.google.api.services.calendar.Calendar$Builder.build(Calendar.java:4603)
    at cl.packg.Quickstart.main(Quickstart.java:162)

其他信息:

对于 HTTP_TRANSPORT 变量,我使用的是:com.google.api.client.http.HttpTransport

对于 JSON_FACTORY 变量,我使用的是:com.google.api.client.json.JsonFactory

对于凭据变量,我使用的是:com.google.api.client.auth.oauth2.Credential

由于

1 个答案:

答案 0 :(得分:0)

Calendar insert method中,提供了创建日历的示例:

 Calendar service = new Calendar.Builder(httpTransport, jsonFactory, credentials)
        .setApplicationName("applicationName").build();

    // Create a new calendar
    com.google.api.services.calendar.model.Calendar calendar = new Calendar();
    calendar.setSummary("calendarSummary");
    calendar.setTimeZone("America/Los_Angeles");

    // Insert the new calendar
    Calendar createdCalendar = service.calendars().insert(calendar).execute();

    System.out.println(createdCalendar.getId());

您的代码似乎缺少.setApplicationName("applicationName")部分。