获得 401“未经授权”从 Android 使用 SignPost 调用 Google Docs API 时
我能够在 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
OAuth 1 还要求对 POST 主体进行签名。
OAuth 1 requires that POST bodies also be signed.