2012-08-08 128 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); 
     }