开发者

gdata unknown authorization header

I'm writing a web app to autopost on google buzz.

I wrote a C# library to manage with "Oauth dance" and in it works fine, I can get oauth_token and oauth_token_secret.

I used www.googlecodesamples.com/oauth_playground/ to validate my oauth_token and oauth_token_secret and it works fine. I tested it with GET and https://www.googleapis.com/buzz/v1/activities/@me/@self to get user's stream, it works.

BUT now

I'm trying to do the same using my C# library but I get always this error:

<?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>

My request header is the same of the one开发者_StackOverflow中文版 from 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

Here's the C# code:

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();
}

Why with the same data it works fine on playground and does not work in C# code? Any idea how to fix it?

Thanks, Stefano


It looks like you are adding OAuth Authorization in your header twice.

In your header string you have string headAuth = "Authorization: OAuth and when you add the header to req1.Headers.Add("Authorization", headAuth); you're adding it again.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜