1

POSTリクエストを使用してGoogleカレンダーで新しいイベントを作成しようとしていますが、常に400エラーが発生します。

これまでのところ私はこれを持っています:

String url = "https://www.googleapis.com/calendar/v3/calendars/"+ calendarID + "/events?access_token=" + token;
String data = "{\n-\"end\":{\n\"dateTime\": \"" + day + "T" + end +":00.000Z\"\n},\n" +
                        "-\"start\": {\n \"dateTime\": \"" + day + "T" + begin + ":00.000Z\"\n},\n" +
                        "\"description\": \"" + description + "\",\n" +
                        "\"location\": \"" + location + "\",\n" +
                        "\"summary\": \"" + title +"\"\n}";



System.out.println(data);

URL u = new URL(url);
HttpURLConnection connection = (HttpURLConnection) u.openConnection();           
connection.setDoOutput(true);
connection.setDoInput(true);
connection.setInstanceFollowRedirects(false); 
connection.setRequestMethod("POST"); 
String a = connection.getRequestMethod();
connection.setRequestProperty("Content-Type", "application/json"); 
connection.setRequestProperty("Accept-Charset", "utf-8");
connection.setRequestProperty("Authorization", "OAuth" + token); 
connection.setUseCaches(false);
DataOutputStream wr = new DataOutputStream(connection.getOutputStream ());
wr.writeBytes(data);
wr.flush();

// Get the response
BufferedReader rd = new BufferedReader(new InputStreamReader(connection.getInputStream()));
    String line;
while ((line = rd.readLine()) != null) {
   System.out.println(line);
}
wr.close();
rd.close();

しかし、応答を読み取るためにBufferedReaderを作成すると、400エラーが発生します。どうしたの?

前もって感謝します!

4

1 に答える 1

4

Java用のGoogleAPIクライアントライブラリを使用してみましたか?これにより、このような操作がはるかに簡単になります。クライアントライブラリを構成してサービスオブジェクトを作成すると、API呼び出しを比較的簡単に行うことができます。この例では、イベントを作成してカレンダーに挿入します。

Event event = new Event();

event.setSummary("Appointment");
event.setLocation("Somewhere");

ArrayList<EventAttendee> attendees = new ArrayList<EventAttendee>();
attendees.add(new EventAttendee().setEmail("attendeeEmail"));
// ...
event.setAttendees(attendees);

Date startDate = new Date();
Date endDate = new Date(startDate.getTime() + 3600000);
DateTime start = new DateTime(startDate, TimeZone.getTimeZone("UTC"));
event.setStart(new EventDateTime().setDateTime(start));
DateTime end = new DateTime(endDate, TimeZone.getTimeZone("UTC"));
event.setEnd(new EventDateTime().setDateTime(end));

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

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

ここで概説するように、サービスオブジェクトを作成したことを前提としています。

于 2012-07-27T22:45:16.607 に答える