2010-12-13 56 views
3

我使用C#编写的.NET应用程序,使用OAuth将照片上传到TwitPic。使用TwitPic + OAuth上传一张照片+鸣叫Twitter(.NET C#) - 为什么没有鸣叫?

oAuth的东西有点棘手。我发现了两个.NET代码来处理它,但是也不满意。 DotNetOpenAuth似乎很沉重,超过我的需要。 (只想做oAuth签名和令牌请求)。代码似乎让我感到困惑和不雅。我必须将6个字符串参数传递给方法,如果我发现任何错误,那么对我来说是祸患。

所以我写了一些代码来自己做,它相当小,它似乎工作。它有效地获取“请求令牌”。它用于弹出授权网页并获取“访问令牌”。它也可以将照片上传到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事务中发布tweet消息,直接发送到Twitter。是吧?我认为oAuth的邮件目的是让消费者代表我做一件事 - 比如允许TwitPic为我发布推文。

任何提示?


编辑
我在这里学习多一点。来自2010年5月的This blog post向我暗示,使用X-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消息的例子。任何人?


UPDATE
验证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算法适用于所有其他请求。

回答

1

我不知道最终的答案,但我认为在Raffi's blog post of May 2010中描述为即将到来的“Real Soon Now”的变化实际上并未实现,无论出于何种原因。

所以实际上,使用OAuth,你必须单独发布到TwitPic和Twitter。

虽然在Twitter中这样做并不困难。只需在状态/ update.xml URL上执行POST即可。

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); 
    } 
} 

该代码有两个棘手的部分:第一个是UrlEncode()调用。 OAuth指定urlencoding必须使用大写字母。内置的.NET例程使用小写字母。所以一定要注意。

第二个棘手的部分是获取OAuth授权标头。如果您使用OAuth库包,它应该非常简单。对于那些想要一个简单的,你可以get one here: OAuth.cs。 (获取OAuthManager下载)

+0

仅供参考 - 我喜欢您的简单OAuth库。我已经做了一个快速修复来处理扩展字符,并将其发布到此处:http://cropperplugins.codeplex.com/discussions/249415 – russau 2011-03-14 03:56:12

+0

只需跟进 - 谢谢,您的更改现在位于OAuth库中;此外,现在有一个DLL,任何人都可以下载。 – Cheeso 2011-04-25 12:23:38

1

我一直在努力使用Twitpic API来生成oAuth集成,并发现我可以将图片发布和Twitter评论与Twitpic图片一起发送。此图像张贴到我们的Eplixo(http://eplixo.com/m/)视频聊天和Twitter客户端的twitter用户帐户

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

这是我的。它是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; 
    } 

}