我正在使用OAuth从用 C# 编写的 .NET 应用程序将照片上传到 TwitPic。
oAuth 的东西有点棘手。我找到了两段 .NET 代码来处理它,但都不满意。 DotNetOpenAuth似乎很重,超出了我的需要。(只想做 oAuth 签名和令牌请求)。OAuthBase.cs代码对我来说似乎很混乱和不雅。我必须将 6 个字符串参数传递给方法,如果出现任何问题,那我就倒霉了。
所以我自己写了一些代码来做,它相当小而且似乎可以工作。它用于获取“请求令牌”。它可以弹出授权网页并获取“访问令牌”。它还可以将照片上传到 TwitPic。
所有 HTTP 响应都返回 200 或 201。
上传 HTTP 消息如下所示:
POST http://api.twitpic.com/2/upload.json HTTP/1.1
Content-Type: multipart/form-data; boundary=48cb9a6d-1f1d-432d-b6e3-307e32e8228a
X-Auth-Service-Provider: https://api.twitter.com/1/account/verify_credentials.json
X-Verify-Credentials-Authorization: OAuth realm="http://api.twitter.com/",
oauth_consumer_key="Dv1er93yKzEMn74hZfPmJA",
oauth_nonce="51fi305k",
oauth_signature="4oWcmZcd%2F%2F81JslJ70xFXFm8%2BQs%3D",
oauth_signature_method="HMAC-SHA1",
oauth_timestamp="1292277715",
oauth_token="59152613-z8EP4GoYS1Mgo3E29JfIqBnyTRlruAJs8Bkvs3q0T",
oauth_version="1.0"
Host: api.twitpic.com
Content-Length: 14605
Expect: 100-continue
Connection: Keep-Alive
--48cb9a6d-1f1d-432d-b6e3-307e32e8228a
Content-Disposition: file; name="media"; filename="CropperCapture[10].jpg"
Content-Type: image/jpeg
....
--48cb9a6d-1f1d-432d-b6e3-307e32e8228a
Content-Disposition: form-data; name="key"
twitpic-api-key-here
--48cb9a6d-1f1d-432d-b6e3-307e32e8228a
Content-Disposition: form-data; name="message"
uploaded from Yappy. (at 12/13/2010 5:01:55 PM)
--48cb9a6d-1f1d-432d-b6e3-307e32e8228a--
我从上传返回的 json 是这样的:
{"id":"3f0jeiw5",
"text":"uploaded from Yappy. (at 12\/13\/2010 5:01:55 PM)",
"url":"http:\\/twitpic.com\/3f0jeiw5",
"width":257,
"height":184,
"size":14156,
"type":"jpg",
"timestamp":"Mon, 13 Dec 2010 22:02:06 +0000",
"user":{
"id":54591561,"screen_name":"bfavre"}
}
但问题是,在我将图像上传到 Twitpic 后,该图像在 TwitPic 上可用,但相关消息从未出现在 Twitter 上。
是什么赋予了?
我在一篇随机博客文章中读到,使用 TwitPic+oAuth 要求我在单独的 HTTP 事务中发布推文消息,直接发送到 Twitter。嗯?我认为 oAuth 的邮件目的是允许消费者代表我做事——比如允许 TwitPic 为我发布推文。
有什么提示吗?
编辑
我在这里学到了更多。 这篇 2010 年 5 月的博文建议我使用 forX-Auth-Service-Provider
的值https://api.twitter.com/1/account/verify_credentials.json
告诉 TwitPic 在收到我的请求时在 twitter.com 上调用“verify_credentials.json”。如果它真的只是在验证我的凭据,这将解释为什么没有发布推文。
该帖子还建议将其换出并替换为https://api.twitter.com/1/status/update.json
应该允许我通过 TwitPic 更新 Twitter 并授权。但这是一篇具有前瞻性的文章——它说获得这种能力需要 Twitter 的工作。
我还没有找到执行此操作的示例 HTTP 消息。任何人?
更新
在将验证 URL 转换为https://api.twitter.com/1/status/update.json
并使用 POST 作为签名后,我得到一个 401 响应代码:
{"errors":
[{"code":401,
"message":"Could not authenticate you (header rejected by twitter)."}]
}
这与 twitter 开发论坛中描述的问题基本相同。该线程末尾的建议是签名计算算法是错误的,但我认为这是不正确的,因为我的 sig 算法适用于所有其他请求。