我正在尝试使用 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 错误。怎么了?
提前致谢!