I'm playing with the google calendar API. Out of curiosity I didn't use the Google libraries, so I can write it all myself. I can't make a new calendar. The authentication is working because this get request is working: https://developers.google.com/google-apps/calendar/v3/reference/calendarList/list
For making a new calendar I used: https://developers.google.com/google-apps/calendar/v3/reference/calendarList/insert
I wrote:
try {
DefaultHttpClient httpclient = new DefaultHttpClient();
httpclient.getParams().setParameter(ClientPNames.HANDLE_REDIRECTS,false);
HttpPost httppost = new HttpPost("https://www.googleapis.com/calendar/v3/users/me/calendarList");
httppost.addHeader("Authorization", "Bearer " + token); //authentication
httppost.addHeader("Host", "googleapis.com");
StringEntity params = new StringEntity("{\"id\": \"TestSchedule\",\"defaultReminders\":[{\"method\": \"popup\",\"minutes\":10}]}");
httppost.setEntity(params);
//EXECUTE REQUEST
HttpResponse postResponse = httpclient.execute(httppost);
//RESPONSE
HttpEntity entity = postResponse.getEntity();
InputStream stream = entity.getContent();
BufferedReader br = new BufferedReader(new InputStreamReader(stream,"UTF-8"));
String line;
while ((line = br.readLine()) != null) {
System.out.println(line);
}
httppost.releaseConnection();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Further the scope to get the right token is: https://www.googleapis.com/auth/calendar This should be correct. I think the problem is in the body of my post request?
Thus, get requests are working so I assume my authentication procedure is correct and I get a correct token. Doing this post request instead of the get request results is a 302 response code. The new URL I get is the main page of google, according to the reference pages I should get a calendar resource.
The following code working. please u can check it now.
{
DefaultHttpClient httpclient = new DefaultHttpClient();
httpclient.getParams().setParameter(ClientPNames.HANDLE_REDIRECTS,false);
HttpGet httpget = new HttpGet("https://www.googleapis.com/calendar/v3/users/me/calendarList");
httpget.addHeader("Authorization", "Bearer " + accessToken);
//HttpPost httppost = new HttpPost("https://www.googleapis.com/calendar/v3/users/me/calendarList");
//httppost.addHeader("Authorization", "Bearer " + accessToken); //authentication
// httppost.addHeader("Host", "googleapis.com");
/* StringEntity params = new StringEntity("{\"id\":}");
httppost.setEntity(params);*/
//EXECUTE REQUEST
HttpResponse postResponse = httpclient.execute(httpget);
//RESPONSE
HttpEntity entity = postResponse.getEntity();
String responseBody1 = EntityUtils.toString(entity);
logger.debug(responseBody1);
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.