2

Id で指定された特定のカレンダーに CalendarEvent を挿入するにはどうすればよいですか?

私のアプリでは、すべてのカレンダーをスキャンして、次のように ArrayList に追加しています。

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

私が見つけた唯一のコードフラグメントは次のとおりです。

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

"private"の代わりにフィードの URL を入れてみました。

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"
}
4

2 に答える 2

2

次のコード スニペットを試してください。

スニペット 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);
}

スニペット 1 は 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());

}

スニペット 2 は、カレンダーのリストを取得し、そのうちの 1 つの ID を取得して、そのカレンダーにイベントを作成します。プライマリ カレンダーにイベントを追加する場合、プライマリ カレンダーの ID は、ログインに使用される Gmail ID です。

詳細なドキュメントは次の場所にあります。

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

于 2013-01-05T13:40:51.883 に答える
0

これは、ユーザーが Google ユーザーであるが、Google プラスに接続していない場合によく発生します。

この呼び出しでも爆発するはずです: service.people().get("me").execute();

于 2013-08-22T22:03:10.543 に答える