【发布时间】:2016-03-09 01:27:17
【问题描述】:
[2011-09-21 11:19 更新] 我已经修复了一个导致问题的问题,那就是 GMT/GMT+2 我的当地时间是 GMT+2,而 Twitter 使用的是 GMT+0/UTC。这导致了时间戳越界错误。
我现在已经解决了这个问题,现在面临下一个问题: string(96) "{"request":"/1/statuses/update_with_media.json","error":"无法通过 OAuth 进行身份验证。"}"
这是通过 Themattharris PHP 库生成的请求头(使用 curl):
["request_header"]=>
string(587) "POST /1/statuses/update_with_media.json HTTP/1.1
User-Agent: themattharris' HTTP Client
Host: upload.twitter.com
Accept: */*
Authorization: OAuth oauth_consumer_key="L9AWIB6jvxm3Req1XWHxJA", oauth_nonce="9e41eea04e2dcc2b67784b35c27d8740", oauth_signature="N2Y4YzYwMDg1YjBmZTA1NDgwNDdjOGY3MDI4YjdiNWE3M2E5ZTA5YQ%3D%3D", oauth_signature_method="HMAC-SHA1", oauth_timestamp="1316596483", oauth_token="373846001-bJYH4C47vk34yfIH3XbpvvfZjd2JrIX9OlO5MyO2", oauth_version="1.0"
Content-Length: 10024
Content-Type: multipart/form-data; boundary=----------------------------3ddd9c41c091
"
我相当确定我使用了正确的秘密(从我的应用程序的 twitter 开发页面复制粘贴)在那里我还生成了我的私人秘密令牌
[更新 2011-09-21 ~10:45] 所以我已经使用 PHP 库(重写为 C++/CLI),我的 C++ 库现在产生与 PHP 库完全相同的结果。 (授权头字符串)。
由于某种原因,我不断收到状态:(401) Unauthorized。 我正在使用 System.Net.WebClient 发出请求。并使用 UploadValues 来上传值。
如果我使用 UploadData 之类的东西或使用 WebRequest 而不是 WebClient,我会收到 Status:(500) Internal Server 错误。
如果需要,我可以提供我生成的所有字符串和标题(但我当然不会泄露秘密)
这是例如我的基本字符串和授权字符串的样子:
基本字符串: POST&https%3A%2F%2Fupload.twitter.com%2F1%2Fstatuses%2Fupdate_with_media.json&oauth_consumer_key%3DL9AWIB6jvxm3Req1XWHxJA%26oauth_nonce%3Ddb0ca1f6c926c1d3ae0d9cbb2c349ae2%26oauth_signature_method%3DHMAC-SHA1%26oauth_timestamp%3D1316538778%26oauth_token%3D373846001-bJYH4C47vk34yfIH3XbpvvfZjd2JrIX9OlO5MyO2%26oauth_version%3D1.0
授权:OAuth oauth_consumer_key="L9AWIB6jvxm3Req1XWHxJA", oauth_nonce="db0ca1f6c926c1d3ae0d9cbb2c349ae2", oauth_signature="OGI4ZDVkYjZjNjA3YWMyZGYxNWYzZDBhNDE0ODcwMzRkMDE4OWZiYQ%3D%3D", oauth_signature_method="HMAC-SHA1", oauth_timestamp="1316538778", oauth_token="373846001-bJYH4C47vk34yfIH3XbpvvfZjd2JrIX9OlO5MyO2", oauth_version="1.0"
以下是旧文本,不再有效。
我可能是一个简单的问题,但我就是想不通。 我正在使用Twitterizer 来获取所需的秘密令牌。该库还提供了一种使用新推文更新推特的方法,但它不支持新的 update_with_media 调用。
我尝试了几种方法,一种是使用 WebRequest,另一种是使用 WebClient(伪 C# 代码):
string URL = "https://upload.twitter.com/1/statuses/update_with_media.json?oauth_consumer_secret=[mysecretgoeshere]&oauth_token_secret=[usersecretgoeshere]";
System.Net.ServicePointManager.Expect100Continue = false;
Byte[] fileData = File.ReadAllBytes(pathToFile);
#if useWebRequest
string postString = "status={statusgoeshere}&image[]={Encoding.ASCII.GetString(fileData)}";
Byte[] postData = Encoding.ASCII.GetBytes(postString);
WebRequest wr = WebRequest.Create(URL);
wr.Method = "POST";
wr.ContentType = "multipart/form-data"; //as required by twitter API
wr.ContentLength = postData.Length;
IO.Stream rqstream = wr.GetRequestStream();
rqstream.Write(postData,0,postData.Length);
rqstream.Close();
wr.GetResponse(); //returns status 500 internal server error
#else
WebClient client = new WebClient();
NameValueCollection postData = new NameValueCollection();
postData.Add("status", "{statusgoeshere}");
postData.add("media[]", Encoding.ASCII.GetString(fileData));
client.UploadValues(URL, "POST", postData); //returns status 401 not authorized
#endif
我想知道我做错了什么(如果我做错了什么)。在我看来,这两种方式似乎都有效,但我可能会遗漏一些东西。
编辑1: 我注意到我以错误的方式传递 oath 参数,将尝试更新该部分。
【问题讨论】:
-
我已经用更多更新的相关信息更新了这个问题。
-
OAuth 密钥值不会通过 Internet 发送。它们用于生成签名(以及所有其他查询字符串和/或表单值)。
-
@RickySmith 是的,我在使用 tmhOAuth PHP 库后发现了这一点;-)
标签: .net twitter twitter-oauth