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

发布于 2024-09-30 12:04:26 字数 1497 浏览 0 评论 0原文

我能够在 Android 中使用 OAuth 和 SignPost 成功调用 GET 并从 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);

-->401 未经授权

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/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 Unauthorized

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

旧时光的容颜 2024-10-07 12:04:26

OAuth 1 还要求对 POST 主体进行签名。

OAuth 1 requires that POST bodies also be signed.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文