2010-10-31 49 views
0

我能夠成功地調用GET,並使用Android中的SignPost中的OAuth使用Google文檔列表API和Spreadsheets API獲取文檔和電子表格列表。然而,我已經花了數小時試圖做出一個成功的POST電話,並繼續得到401。我會很感激任何提示或意見。由於使用Android中的SignPost調用Google Docs API時獲取401「未授權」

下面是代碼:

//I get the secret and token successfully 
CommonsHttpOAuthConsumer consumer = new CommonsHttpOAuthConsumer(C.OAuth.CONSUMER_KEY,   C.OAuth.CONSUMER_SECRET); 
consumer.setMessageSigner(new HmacSha1MessageSigner()); 
consumer.setTokenWithSecret(token, secret); 

//Set the Docs Uri 
String url = consumer.sign("https://docs.google.com/feeds/default/private/full"); 

HttpPost post = new HttpPost(url); 

//Add headers   
post.addHeader("user-agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.2; .NET CLR 1.0.3705;)"); 
post.addHeader("Host","docs.google.com"); 
post.addHeader("Accept","*/*"); 
post.addHeader("Content-Type","application/atom+xml"); 
post.addHeader("GData-Version","3.0"); 

//Create and add post body   
String reqBody="<?xml version='1.0' encoding='UTF-8'?>"+ 
"<entry xmlns=\"http://www.w3.org/2005/Atom\">"+ 
"<category scheme=\"http://schemas.google.com/g/2005#kind\" term=\"http://schemas.google.com/docs/2007#document\"/>"+ 
"<title>new document</title>"+ 
"</entry>";   

StringEntity se = new StringEntity(reqBody,"UTF-8"); 
post.setEntity(se); 

//Create and execute the Client 
HttpClient client = new DefaultHttpClient(); 
HttpResponse response = client.execute(post); 

- > 401未授權

+0

我在獲取電子表格內容時也遇到了錯誤401。文檔/圖紙/等文件工作正常。 – 2012-04-30 14:33:29

回答