gdata未知授权标头

Ste*_*ano 5 c# gdata-api gdata

我正在写一个网络应用程序来自动搜索谷歌的嗡嗡声.

我写了一个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>
Run Code Online (Sandbox Code Playgroud)

我的请求标题与操场上的标题相同.

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
Run Code Online (Sandbox Code Playgroud)

这是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();
}
Run Code Online (Sandbox Code Playgroud)

为什么使用相同的数据它在游乐场工作正常并且无法在C#代码中工作?知道怎么解决吗?

谢谢,斯特凡诺

Joh*_*ade 0

您似乎在标头中添加了两次 OAuth 授权。

在您的标头字符串中,string headAuth = "Authorization: OAuth当您添加标头时,req1.Headers.Add("Authorization", headAuth);您将再次添加它。