【问题标题】:Insert CalendarEvent to other calendar than "primary"将 CalendarEvent 插入“主要”以外的其他日历
【发布时间】:2012-12-21 07:36:46
【问题描述】:

如何将 CalendarEvent 插入到由其 Id 给出的特定日历中?

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

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

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

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

我试图把提要 url 代替 "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"
}

【问题讨论】:

  • 您可以在“APIs Explorer”中查看您的示例用法。日历方法的链接:developers.google.com/apis-explorer/#p/calendar/v3。测试一下——也许你会在那之后找到方法。
  • 看起来您可能正在混合版本 - 提要 URL 来自 v2,但 JSON 响应是 v3。你能澄清一下你的代码中那个 URL 来自哪里吗?

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


【解决方案1】:

试试这些代码 sn-ps:

片段1:

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);
}

片段一负责 OAuth 并将程序重定向到重定向 uri。变量 scope、client_id、cliend_secret 和 scope 的值来自 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());

}

片段二获取日历列表并获取其中一个的 id 并在该日历中创建一个事件。如果您想将事件添加到主日历,则主日历的 id 是用于登录的 gmail id。

更多文档可在此处找到:

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

【讨论】:

    【解决方案2】:

    当用户是 google 用户但未连接到 google plus 时,通常会发生这种情况。

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-01-09
      • 2019-07-28
      • 1970-01-01
      • 1970-01-01
      • 2017-09-06
      • 2012-12-15
      • 1970-01-01
      • 2014-04-25
      相关资源
      最近更新 更多