GData未知的授权头问题

5

我正在编写一个用于自动发布到Google Buzz的Web应用程序。

我编写了一个C#库来处理“Oauth dance”,它很好地工作,我可以获取oauth_token和oauth_token_secret。

我使用www.googlecodesamples.com/oauth_playground/验证了我的oauth_token和oauth_token_secret,它也正常工作。我测试了GET和https://www.googleapis.com/buzz/v1/activities/@me/@self以获取用户的流,这也正常。

但是,现在

我正在尝试使用我的C#库进行相同的操作,但我总是遇到以下错误:

<?xml version="1.0" encoding="UTF-8"?>
<errors xmlns="http://schemas.google.com/g/2005">
   <error>
      <domain>GData</domain>
      <code>invalid</code>
      <location type="header">Authorization</location>
      <internalReason>Unknown authorization header</internalReason>
   </error>
</errors>

我的请求头与playground的请求头相同。

Accept: */*
Content-Type: application/atom+xml
Authorization: OAuth oauth_version="1.0", oauth_nonce="9216320",
oauth_timestamp="1283430867", oauth_consumer_key="www.mysite.com",
oauth_token="1%2FZodlNmPP96GT11vYaWA0y6QoqKLqNqZ8bNmxknZZZc",
oauth_signature_method="HMAC-SHA1",
oauth_signature="Tuu82feKNWa4CxoDUyvtIEVODRA%3D"
GData-Version: 2.0

以下是C#代码:

string headAuth = "Authorization: OAuth oauth_version=\"1.0\", oauth_nonce=\"9216320\", oauth_timestamp=\"1283430867\",
oauth_consumer_key=\"www.mysite.com\", oauth_token=
\"1%2FZodlNmPP96GT11vYaWA0y6QoqKLqNqZ8bNmxknZZZc\",
oauth_signature_method=\"HMAC-SHA1\", oauth_signature=
\"Tuu82feKNWa4CxoDUyvtIEVODRA%3D\"";

HttpWebRequest req1 =(HttpWebRequest)HttpWebRequest.Create("https://www.googleapis.com/buzz/v1/activities/@me/@self");
req1.Method = "GET";
req1.Accept = "*/*";
req1.ContentType = "application/atom+xml";
req1.Headers.Add("Authorization", headAuth);
req1.Headers.Add("GData-Version", "2.0");

try
{
    HttpWebResponse response1 =(HttpWebResponse)req1.GetResponse();
    using (var sr = new StreamReader(response1.GetResponseStream()))
    {
        string test_1 = sr.ReadToEnd();
    }
}
catch (WebException e)
{
    Stream objStream = e.Response.GetResponseStream();
    StreamReader objStreamReader = new StreamReader(objStream);
    string err = objStreamReader.ReadToEnd();
}

为什么在Playground上相同的数据可以正常工作,但在C#代码中却无法正常工作?有任何想法如何解决吗?

谢谢, Stefano


1
你解决了吗?因为我也在使用配置API时遇到了同样的问题。 - JochenJung
1个回答

0

看起来你在头部两次添加了OAuth授权。

在你的头部字符串中,你有string headAuth = "Authorization: OAuth,当你将头部添加到req1.Headers.Add("Authorization", headAuth);时,你又再次添加了它。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接