Click here to Skip to main content
15,881,027 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
i have this code, and i want to upload image to twitter,

how can i do it ?

C#
string twitterURL = "https://api.twitter.com/1.1/statuses/update_with_media.json";


       BLEnumHelper m_BLEnumHelper = new BLEnumHelper();
       BLGodsProfile userprofile = new BLGodsProfile();
       tbmgodsprofile godsAccessToken = userprofile.GetGodsByIdGods(m_strIdGods);

       string oauth_token = godsAccessToken.twittertoken;
       string oauth_token_secret = godsAccessToken.twitterpin;
       string oauth_consumerKey = System.Configuration.ConfigurationManager.AppSettings["TwitterConsumerKey"];
       string oauth_consumerSecret = System.Configuration.ConfigurationManager.AppSettings["TwitterConsumerSecret"];

       string oauth_version = "1.0";
       string oauth_signature_method = "HMAC-SHA1";

       string oauth_nonce = Convert.ToBase64String(new ASCIIEncoding().GetBytes(DateTime.Now.Ticks.ToString()));
       System.TimeSpan timeSpan = (DateTime.UtcNow - new DateTime(1970, 1, 1, 0, 0, 0, 0, DateTimeKind.Utc));
       string oauth_timestamp = Convert.ToInt64(timeSpan.TotalSeconds).ToString();

       // create oauth signature
       string baseFormat = "oauth_consumer_key={0}&oauth_nonce={1}&oauth_signature_method={2}" + "&oauth_timestamp={3}&oauth_token={4}&oauth_version={5}&status={6}";

       string baseString = string.Format(
           baseFormat,
           oauth_consumerKey,
           oauth_nonce,
           oauth_signature_method,
           oauth_timestamp, oauth_token,
           oauth_version,
           Uri.EscapeDataString(message)
       );

       string oauth_signature = null;
       using (HMACSHA1 hasher = new HMACSHA1(ASCIIEncoding.ASCII.GetBytes(Uri.EscapeDataString(oauth_consumerSecret) + "&" + Uri.EscapeDataString(oauth_token_secret))))
       {
           oauth_signature = Convert.ToBase64String(hasher.ComputeHash(ASCIIEncoding.ASCII.GetBytes("POST&" + Uri.EscapeDataString(twitterURL) + "&" + Uri.EscapeDataString(baseString))));
       }

       // create the request header
       string authorizationFormat = "OAuth realm=\"{7}\", oauth_consumer_key=\"{0}\", oauth_nonce=\"{1}\", " + "oauth_signature=\"{2}\", oauth_signature_method=\"{3}\", " + "oauth_timestamp=\"{4}\", oauth_token=\"{5}\", " + "oauth_version=\"{6}\"";

       string authorizationHeader = string.Format(
           authorizationFormat,
           Uri.EscapeDataString(oauth_consumerKey),
           Uri.EscapeDataString(oauth_nonce),
           Uri.EscapeDataString(oauth_signature),
           Uri.EscapeDataString(oauth_signature_method),
           Uri.EscapeDataString(oauth_timestamp),
           Uri.EscapeDataString(oauth_token),
           Uri.EscapeDataString(oauth_version),
           Uri.EscapeDataString(twitterURL)
       );

       HttpWebRequest objHttpWebRequest = (HttpWebRequest)WebRequest.Create(twitterURL);
       objHttpWebRequest.ContentType = "application/x-www-form-urlencoded";
       objHttpWebRequest.Headers.Add("Authorization", authorizationHeader);
       objHttpWebRequest.Method = "POST";
       using (Stream objStream = objHttpWebRequest.GetRequestStream())
       {
           byte[] content = ASCIIEncoding.ASCII.GetBytes("status=" + Uri.EscapeDataString(message));
           content = ASCIIEncoding.ASCII.GetBytes("media[]=" + @"D:\TgByPiwi\TG\TgUi\images\chartshare\24jpeg");
           objStream.Write(content, 0, content.Length);
       }

       var responseResult = "";
       try
       {
           //success posting
           WebResponse objWebResponse = objHttpWebRequest.GetResponse() as HttpWebResponse;
           StreamReader objStreamReader = new StreamReader(objWebResponse.GetResponseStream());
           responseResult = objStreamReader.ReadToEnd().ToString();
       }
       catch (Exception ex)
       {
           //throw exception error
           responseResult = "Twitter Post Error: " + ex.Message.ToString() + ", authHeader: " + authorizationHeader;
       }


it's seem like i lost something in this code, cause, i can't upload it, and also not getting some error code.
Posted

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900