.net - 使用 TwitPic + OAuth 将照片 + 推文上传到 Twitter (.NET C#) - 为什么没有推文?

标签 .net twitter oauth twitpic

我正在使用 OAuth 将照片上传到 TwitPic ,来自用 C# 编写的 .NET 应用程序。

oAuth 的东西有点棘手。我找到了两段 .NET 代码来处理它,但对两者都不满意。 DotNetOpenAuth似乎很重,超出了我的需要。 (只想进行 oAuth 签名和 token 请求)。 OAuthBase.cs代码对我来说似乎很困惑而且不优雅。我必须将 6 个字符串参数传递给方法,如果我有任何顺序不正确,我就会倒霉。

所以我自己写了一些代码来完成它,它相当小而且似乎可以工作。它用于获取“请求 token ”。它可以弹出授权网页并获取“访问 token ”。它还可以将照片上传到 TwitPic。

所有 HTTP 响应返回为 200 或 201。

upload HTTP message看起来像这样:

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

什么给了?

我读入了a random blog post使用 TwitPic+oAuth 需要我在单独的 HTTP 事务中发布推文消息,直接发送到 Twitter。嗯?我认为 oAuth 的邮件目的是允许消费者代表我做事——比如允许 TwitPic 为我发布推文。

有什么提示吗?


编辑
我在这里学到了更多。 This blog post从 2010 年 5 月开始,我建议使用 https://api.twitter.com/1/account/verify_credentials.jsonX-Auth-Service-Provider 值告诉 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)."}]
}

这与描述的问题基本相同 here, in the twitter dev forum .该线程末尾的建议是签名计算算法是错误的,但我认为这是不正确的,因为我的 sig 算法适用于所有其他请求。

最佳答案

我一直致力于使用 Twitpic API 生成 oAuth 集成,发现我可以从 Twitpic 获取图像发布和 Twitter 评论与图像一起发送。这会将图像发布到我们的 Eplixo (http://eplixo.com/m/) 视频聊天和推特客户端的推特用户帐户

但是我似乎无法得到回应。现在我可以忍受它的发布和上传,但弄清楚如何获取应用程序其他部分的响应数据会很有用。

这是我的。它是 Twipli API 包装器的变体

protected void Button1_Click(object sender, EventArgs e)
{

    string ct = img.PostedFile.ContentType.ToString();
    string usertoken = Session["usrToken"].ToString();
    string userSecret = Session["usrSecret"].ToString();
    string conkey = Session["ConsumerKey"].ToString();
    string consecret = Session["ConsumerSecret"].ToString();
    string twitkey = Session["twitpickey"].ToString();

    string _m = m.Text; // This takes the Tweet to be posted


    HttpPostedFile myFile = img.PostedFile;
    string fileName = myFile.FileName.ToString();

    int nFileLen = myFile.ContentLength;
    byte[] myData = new byte[nFileLen];
    myFile.InputStream.Read(myData, 0, nFileLen);

    TwitPic tw = new TwitPic();
    upres.Text = tw.UploadPhoto(myData, ct, _m, fileName, twitkey, usertoken, userSecret, conkey, consecret).ToString();
    Response.Redirect("twittercb.aspx?oauth_verifier=none");
}
public class TwitPic
{
    private const string TWITPIC_UPLADO_API_URL = "http://api.twitpic.com/2/upload";
    private const string TWITPIC_UPLOAD_AND_POST_API_URL = "http://api.twitpic.com/1/uploadAndPost";
    /// 
    /// Uploads the photo and sends a new Tweet
    /// 
    /// <param name="binaryImageData">The binary image data.
    /// <param name="tweetMessage">The tweet message.
    /// <param name="filename">The filename.
    /// Return true, if the operation was succeded.
    public string UploadPhoto(byte[] binaryImageData, string ContentType, string tweetMessage, string filename, string tpkey, string usrtoken, string usrsecret, string contoken, string consecret)
    {            
        string boundary = Guid.NewGuid().ToString();
        string requestUrl = String.IsNullOrEmpty(tweetMessage) ? TWITPIC_UPLADO_API_URL : TWITPIC_UPLOAD_AND_POST_API_URL;
        HttpWebRequest request = (HttpWebRequest)WebRequest.Create(requestUrl);
        string encoding = "iso-8859-1";

        request.PreAuthenticate = true;
        request.AllowWriteStreamBuffering = true;
        request.ContentType = string.Format("multipart/form-data; boundary={0}", boundary);
        request.Method = "POST";

        string header = string.Format("--{0}", boundary);
        string footer = string.Format("--{0}--", boundary);

        StringBuilder contents = new StringBuilder();
        contents.AppendLine(header);

        string fileContentType = ContentType;
        string fileHeader = String.Format("Content-Disposition: file; name=\"{0}\"; filename=\"{1}\"", "media", filename);
        string fileData = Encoding.GetEncoding(encoding).GetString(binaryImageData);

        contents.AppendLine(fileHeader);
        contents.AppendLine(String.Format("Content-Type: {0}", fileContentType));
        contents.AppendLine();
        contents.AppendLine(fileData);

        contents.AppendLine(header);
        contents.AppendLine(String.Format("Content-Disposition: form-data; name=\"{0}\"", "key"));
        contents.AppendLine();
        contents.AppendLine(tpkey);

        contents.AppendLine(header);
        contents.AppendLine(String.Format("Content-Disposition: form-data; name=\"{0}\"", "consumer_token"));
        contents.AppendLine();
        contents.AppendLine(contoken);

        contents.AppendLine(header);
        contents.AppendLine(String.Format("Content-Disposition: form-data; name=\"{0}\"", "consumer_secret"));
        contents.AppendLine();
        contents.AppendLine(consecret);

        contents.AppendLine(header);
        contents.AppendLine(String.Format("Content-Disposition: form-data; name=\"{0}\"", "oauth_token"));
        contents.AppendLine();
        contents.AppendLine(usrtoken);

        contents.AppendLine(header);
        contents.AppendLine(String.Format("Content-Disposition: form-data; name=\"{0}\"", "oauth_secret"));
        contents.AppendLine();
        contents.AppendLine(usrsecret);

        if (!String.IsNullOrEmpty(tweetMessage))
        {
            contents.AppendLine(header);
            contents.AppendLine(String.Format("Content-Disposition: form-data; name=\"{0}\"", "message"));
            contents.AppendLine();
            contents.AppendLine(tweetMessage);
        }

        contents.AppendLine(footer);            
        byte[] bytes = Encoding.GetEncoding(encoding).GetBytes(contents.ToString());            
        request.ContentLength = bytes.Length;

        string mediaurl = "";
        try
        {
            using (Stream requestStream = request.GetRequestStream()) // this is where the bug is due to not being able to seek.
            {        
                requestStream.Write(bytes, 0, bytes.Length); // No problem the image is posted and tweet is posted
                requestStream.Close();                       
                using (HttpWebResponse response = (HttpWebResponse)request.GetResponse()) // here I can't get the response
                { 
                    using (StreamReader reader = new StreamReader(response.GetResponseStream()))
                    {
                        string result = reader.ReadToEnd();

                        XDocument doc = XDocument.Parse(result); // this shows no root elements and fails here

                        XElement rsp = doc.Element("rsp");
                        string status = rsp.Attribute(XName.Get("status")) != null ? rsp.Attribute(XName.Get("status")).Value : rsp.Attribute(XName.Get("stat")).Value;
                        mediaurl = rsp.Element("mediaurl").Value;
                        return mediaurl;                            
                    } 
                } 

            }
        }
        catch (Exception ex)
        {
            ex.ToString();
        } 
        return mediaurl;
    }

}

关于.net - 使用 TwitPic + OAuth 将照片 + 推文上传到 Twitter (.NET C#) - 为什么没有推文?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4434576/

相关文章:

json - Pig脚本/命令过滤特定字符串上的文件

iOS:即使路径正确,Twitter Stream API 仍为 Expanded_URL 返回 Null?

api - Twitter API request_token

.NET:GC 何时运行?内存泄漏?

.net - Using 语句是否需要 Try/Catch/finally 来处理异常?

c# - IQbservable 示例

.NET 对缓存项 (DataTable) 的引用

php - 用户登录 TWITTER 后获取用户详细信息

authentication - Google oAuth 2.0 本地主机解析

iOS Twitter+OAuth 检查用户是否登录