2012-08-08 129 views
1

我正在玩谷歌日历API。出于好奇,我没有使用Google库,所以我可以自己写。 我无法制作新的日历。 的认证工作,因为这让请求工作:https://developers.google.com/google-apps/calendar/v3/reference/calendarList/listJava:谷歌日历API

为了使新的日历我用:https://developers.google.com/google-apps/calendar/v3/reference/calendarList/insert

我写道:

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

而且范围,以获得正确的令牌是: https://www.googleapis.com/auth/calendar 这应该是正确的。我认为问题出在我的发布请求中?

因此,获取请求正在工作,所以我假设我的身份验证过程是正确的,并且我得到了正确的令牌。做这个发布请求而不是获取请求结果是一个302响应代码。我得到的新网址是谷歌的主页,根据参考页我应该得到一个日历资源。

回答

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