2010-12-13 30 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; 
    } 

}