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 /活动/@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>
我的请求标头与游乐场中的请求标头相同。
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();
}
为什么使用相同的数据在操场上工作正常但不起作用 在 C# 代码中? 知道如何修复它吗?
谢谢, 斯特凡诺
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 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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您似乎在标头中添加了两次 OAuth 授权。
在标头字符串中,您有
string headAuth = "Authorization: OAuth
,当您将标头添加到req1.Headers.Add("Authorization", headAuth);
时,您将再次添加。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 toreq1.Headers.Add("Authorization", headAuth);
you're adding it again.