【问题标题】:Getting a 401 "unauthorized" when calling Google Docs API using SignPost from Android使用 Android 中的 SignPost 调用 Google Docs API 时出现 401“未经授权”
【发布时间】:2011-08-17 03:35:19
【问题描述】:

我能够成功调用 GET 并在 Android 中使用带有 SignPost 的 OAuth 从 Google Documents List API 和 Spreadsheets API 获取文档和电子表格列表。但是,我花了几个小时尝试成功进行 POST 调用并不断收到 401。我将不胜感激任何提示或 cmets。谢谢

代码如下:

//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 未授权

【问题讨论】:

  • 我在获取电子表格内容时也遇到了 err 401。 Docs/drawings/etc 文件工作正常。

标签: oauth signpost


【解决方案1】:

OAuth 1 要求 POST 正文也需要签名。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-12-17
    • 2014-11-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-10-22
    • 1970-01-01
    • 2013-04-02
    相关资源
    最近更新 更多