将CalendarEvent插入除“主要”之外的其他日历

时间:2012-12-21 07:36:46

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

如何将CalendarEvent插入其Id?

指定的特定日历

在我的应用程序中,我正在扫描所有日历并将它们添加到ArrayList中,如下所示:

for (CalendarListEntry calendarEntry : this.calendars.getItems()) {
      this.calendarIDs.add(calendarEntry.getId());
}

我找到的唯一代码片段是:

Event createdEvent = service.events().insert("primary", event).execute();

我尝试将Feed网址替换为“private”,例如

http://www.google.com/calendar/feeds/<id_here>%40group.calendar.google.com/private/full

然后我得到了一个

com.google.api.client.googleapis.json.GoogleJsonResponseException: 404 Not Found
{
  "code" : 404,
  "errors" : [ {
    "domain" : "global",
    "message" : "Not Found",
    "reason" : "notFound"
  }],
  "message" : "Not Found"
}

2 个答案:

答案 0 :(得分:2)

尝试以下代码段:

Snippet1:

public void authenticate(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {

    List <String> scopes = new LinkedList<String>();
    scopes.add(scope);
    AuthorizationCodeRequestUrl authorize = new GoogleAuthorizationCodeRequestUrl(client_id, redirect_uri, scopes);
    authorize.setRedirectUri(redirect_uri);
    String authorize_url              = authorize.build();
    log.info(authorize_url);
    response.sendRedirect(authorize_url);
}

Snippet one负责OAuth并将程序重定向到重定向uri。变量scope,client_id,cliend_secret和范围的值来自Google api控制台。

摘录2:

public void importCalendarList(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
    String code = request.getParameter("code");
    HttpTransport transport = new NetHttpTransport();
    JsonFactory jsonFactory = new JacksonFactory();

    GoogleTokenResponse res = new GoogleAuthorizationCodeTokenRequest(transport, jsonFactory, client_id, client_secret, code, redirect_uri).execute();
    ]String accessToken = res.getAccessToken();
    Calendar.Builder builder = new Calendar.Builder(transport, jsonFactory, null);  
    builder.setCalendarRequestInitializer(new CalendarRequestInitializer(accessToken));
    Calendar calendarService = builder.build();
    Calendar.CalendarList.List list = calendarService.calendarList().list();
    list.setOauthToken(accessToken);
    List <CalendarListEntry>list1=list.execute().getItems();
    String id = list1.get(0).getId();
    p.write(id);

    for(CalendarListEntry temp:list1) {
        p.println(temp.getSummary());
        temp.getId();
    }

    Event e = new Event();

    e.setSummary("Test event");
    e.setLocation("Adaptavant");

    Date startDate = new Date();
    Date endDate = new Date(startDate.getTime() + 3600000);
    DateTime start = new DateTime(startDate, TimeZone.getTimeZone("UTC"));
    e.setStart(new EventDateTime().setDateTime(start));
    DateTime end = new DateTime(endDate, TimeZone.getTimeZone("UTC"));
    e.setEnd(new EventDateTime().setDateTime(end));
    Event insertedEvent = calendarService.events().insert(id, e).setOauthToken(accessToken).execute();

    p.println(insertedEvent.getId());

}

Snippet 2获取日历列表并获取其中一个日历的ID并在该日历中创建一个事件。如果要将事件添加到主日历,则主日历的id是用于登录的gmail ID。

此处可找到更多文档:

https://google-api-client-libraries.appspot.com/documentation/calendar/v3/java/latest/index.html

答案 1 :(得分:0)

这通常发生在用户是谷歌用户但他没有连接谷歌加。

它也应该在这个调用中爆炸:service.people()。get(“me”)。execute();