利用TwitPic + OAuth的上传照片+推到Twitter(.NET C#) - 为什么没有鸣叫? [英] using TwitPic + OAuth to upload a photo + tweet to Twitter (.NET C#) - why no tweet?

查看:291
本文介绍了利用TwitPic + OAuth的上传照片+推到Twitter(.NET C#) - 为什么没有鸣叫?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我上传照片到TwitPic,使用 OAuth的,从.NET应用程序用C#编写。

I'm uploading photos to TwitPic, using OAuth, from a .NET app written in C#.

OAuth的东西是有点棘手。我发现.NET code两个位来处理,但并不满意无论是。 DotNetOpenAuth 似乎是pretty的重,比我更需要。 (只是想做到的OAuth签名和令牌请求)。该 OAuthBase.cs code似乎很乱,不雅给我。我必须通过6字符串参数的方法,如果我有任何失灵,祸哉临到我。

The oAuth stuff is a little tricky. I found two bits of .NET code to handle it, but wasn't satisfied with either. DotNetOpenAuth seemed to be pretty heavy, more than I need. (Just wanna do oAuth signatures and token requests). The OAuthBase.cs code seemed confused and inelegant to me. I had to pass 6 string parameters to methods, and if I got any out of order, woe be unto me.

所以,我写了一些code做我自己,这是相当小的,它似乎工作。它的工作原理获取请求令牌。它的工作原理弹出授权的网页,并获得访问令牌。它也可以将照片上传到TwitPic。

So I wrote some code to do it myself, it's fairly small and it seems to work. It works to acquire "request tokens". It works to pop the authorize web page and to acquire "access tokens". It also works to upload photos to TwitPic.

所有的HTTP响应回来为200或201。

All the HTTP responses come back as 200 or 201.

看起来像这样的上传HTTP消息:

The upload HTTP message looks like this:

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是这样的:

The json I get back from the upload is like this:

{"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上。

But the problem is, after I upload the image to Twitpic, the image is available on TwitPic, but the associated message never appears on Twitter.

怎么办?

我读随机博客文章,使用TwitPic + OAuth的要求我发布的tweet消息在一个单独的HTTP事务,直接到Twitter。是吧?我想到的OAuth的邮件目的是让消费者做的事情我代表我 - 样让TwitPic张贴鸣叫我。

I read in a random blog post that using TwitPic+oAuth requires me to post the tweet message in a separate HTTP transaction, direct to Twitter. huh? I thought the mail purpose of oAuth was to allow consumers to do things on my behalf - like allowing TwitPic to post a tweet for me.

任何提示?

修改
我学习了一点在这里。即使用值<$ C
这从2010年5月的博客文章的建议对我$ C> X-的Auth-服务提供商 https://api.twitter.com/1/account/verify_credentials.json 告诉TwitPic呼吁在twitter.com上verify_credentials.json时,它得到了我的要求。如果它真的只是验证我的凭据,这可以解释为什么没有分享Tweet帐。

EDIT
I'm learning a little more here. This blog post from May 2010 suggests to me that using a value for X-Auth-Service-Provider of https://api.twitter.com/1/account/verify_credentials.json tells TwitPic to call "verify_credentials.json" on twitter.com when it gets my request. If it really IS just verifying my credentials, this would explain why no Tweet is posted.

该帖子还建议,换了这一点,并与 https://api.twitter.com/1/status/update.json 取代它的的允许我通过TwitPic与代表团更新微博​​。但它是一个前瞻性的岗位 - 它说,获得这个功能需要在Twitter上的部分工作。

The post also suggests that swapping that out and replacing it with https://api.twitter.com/1/status/update.json should allow me to update Twitter via TwitPic with delegation. But it is a forward-looking post - it says that getting this capability requires work on Twitter's part.

我还没有找到,这是否又一个例子HTTP消息。有人吗?

I haven't found an example HTTP message that does this yet. Anyone?

更新
转换的验证网址 https://api.twitter.com/1/status/update.json 并使用后的签名后,我得到一个401响应code:

UPDATE
After converting the verify URL to https://api.twitter.com/1/status/update.json and using POST for the signature, I get a 401 response code:

{"errors":
  [{"code":401,
    "message":"Could not authenticate you (header rejected by twitter)."}]
}

这基本上是如所述这里,在同样的问题Twitter的开发论坛。在该线程结束的建议是,签名计算算法是错误的,但我认为这是不正确,因为我的签名算法适用于所有的其他请求。

This is basically the same problem as described here, in the twitter dev forum. The suggestion at the end of that thread was that the signature-computation algorithm is wrong, but I think that's incorrect, as my sig algorithm works with all other requests.

推荐答案

我不知道最终的答案,但我认为这是描述为的 2010年5月的拉菲的博客文章,还没有真正被做了,无论出于何种原因。

I don't know the final answer, but I think that the changes that were described as coming "Real Soon Now" in Raffi's blog post of May 2010, have not actually been made, for whatever reason.

所以,事实上,使用OAuth,你的执行的必须分别张贴到TwitPic和到Twitter。

So in fact, using OAuth, you do have to post separately to TwitPic and to Twitter.

这不是很难做到这一点的微博,虽然。只要做对的状态/ update.xml网址POST。

It's not hard to do this in Twitter, though. Just do a POST on the statuses/update.xml URL.

private void Tweet(string message)
{
    var twitterUpdateUrlBase = "http://api.twitter.com/1/statuses/update.xml?status=";
    var url = twitterUpdateUrlBase + UrlEncode(message);

    var authzHeader = oauth.GenerateAuthzHeader(url, "POST");

    var request = (HttpWebRequest)WebRequest.Create(url);
    request.Method = "POST";
    request.PreAuthenticate = true;
    request.AllowWriteStreamBuffering = true;
    request.Headers.Add("Authorization", authzHeader);

    using (var response = (HttpWebResponse)request.GetResponse())
    {
        if (response.StatusCode != HttpStatusCode.OK)
        MessageBox.Show("There's been a problem trying to tweet:" +
                        Environment.NewLine +
                        response.StatusDescription +
                        Environment.NewLine +
                        Environment.NewLine +
                        "You will have to tweet manually." +
                        Environment.NewLine);
    }
}

有两个棘手的部分是code:首先是UrlEn code()调用。 OAuth的指定URL编码必须使用大写字母。内置的.NET程序使用小写。所以一定要upcase。

There are two tricky parts to that code: First is the UrlEncode() call. OAuth specifies that the urlencoding must use uppercase letters. The built-in .NET routine uses lowercase. So be sure to upcase.

第二个棘手的部分是越来越OAuth的认证头。如果您使用的OAuth库包应该是pretty的简单。对于那些谁想要一个简单的,你可以得到一个位置:OAuth.cs 。 (获取OAuthManager下载)

The second tricky part is is getting the OAuth Authorization Header. If you use an OAuth library package it should be pretty simple. For those who want a simple one, you can get one here: OAuth.cs. (Get the OAuthManager downloaD)

这篇关于利用TwitPic + OAuth的上传照片+推到Twitter(.NET C#) - 为什么没有鸣叫?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆