开发者

Getting a 401 "unauthorized" when calling Google Docs API using SignPost from Android

I am able to call GET successfully and get lists of docs and spreadsheets from Google Documents List API and Spreadsheets API using OAuth with SignPost in Android. However, I have spent hours trying to make a successful POST call and keep getting a 401. I Would appreciate any hints or comments. Thanks

Here is the code:

//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/200开发者_JAVA百科7#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 Unauthorized


OAuth 1 requires that POST bodies also be signed.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜