使用Android的SignPost调用Google Docs API时,获得401“未授权”

时间:2010-10-31 19:05:35

标签: oauth signpost

我可以成功调用GET并使用OAuth和Android中的SignPost从Google Documents List API和Spreadsheets API获取文档和电子表格列表。但是,我花了好几个小时试图成功发布POST电话并继续获得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);

- &gt; 401 Unauthorized

1 个答案:

答案 0 :(得分:0)

OAuth 1要求POST主体也要签名。