Post Image With Message in Twitter Tweet in .NET Framework 3.5

Step 1
 
Add ASP.Net Web Service in 4.0.
 
Step 2
 
For Twitterizer reference use the following procedure:
  • Go to "Tools"
  • Select "Library Package Manager"
  • Select the "Package Manager Console" then select the source as a Nugget official package source and select your project name in the default project
  • Execute command
  • Install-Package twitterizer -Version 2.4.1
Step 3
 
Check the code behind the web service Service1.asmx.cs
  1. public class Service1 : System.Web.Services.WebService  
  2. {  
  3.     string requestToken = string.Empty;  
  4.     string pin = string.Empty;  
  5.     string oauth_consumer_key;  
  6.     string oauth_consumer_secret;  
  7.     [WebMethod]  
  8.     public string HelloWorld()  
  9.     {  
  10.         return "Hello World";  
  11.     }  
  12.     [WebMethod]  
  13.     public OAuthTokenResponse function1(string x)  
  14.     {  
  15.         OAuthTokenResponse reqToken = null;  
  16.         oauth_consumer_key = "abcdefghijk";  
  17.         oauth_consumer_secret = "1234567890123213232";  
  18.    
  19.         if (HttpContext.Current.Request["oauth_token"] == null)  
  20.         {  
  21.             reqToken = OAuthUtility.GetRequestToken(oauth_consumer_key, oauth_consumer_secret, x);  
  22.         }  
  23.    
  24.         return reqToken;  
  25.     }  
  26.     [WebMethod]  
  27.     public bool function2(string requestToken, string pin)  
  28.     {  
  29.         oauth_consumer_key = "abcdefghijk";  
  30.         oauth_consumer_secret = "1234567890123213232";  
  31.         var tokens = OAuthUtility.GetAccessToken(oauth_consumer_key, oauth_consumer_secret, requestToken, pin);  
  32.         OAuthTokens accesstoken = new OAuthTokens()  
  33.         {  
  34.             AccessToken = tokens.Token,  
  35.             AccessTokenSecret = tokens.TokenSecret,  
  36.             ConsumerKey = oauth_consumer_key,  
  37.             ConsumerSecret = oauth_consumer_secret  
  38.         };  
  39.         //~/img/th2.jpg  
  40.         byte[] photo = File.ReadAllBytes(@"D:\MVC_GROUP_DEAL\WebApplication1\WebService1\th22.jpg");  
  41.         TwitterResponse<TwitterStatus> response = TwitterStatus.UpdateWithMedia(accesstoken, "img", photo);  
  42.         if (response.Result == RequestResult.Success)  
  43.         {  
  44.             return true;  
  45.         }  
  46.         else  
  47.         {  
  48.             return false;  
  49.         }  
  50.     }  
Step 4
 
Add a new project. Create an ASP.Net web application in Framework 3.5. Add a default page. In the code behind add the following code.
  1. using Twitterizer;  
  2. using WebApplication1.localhost;  
  3. protected void Page_Load(object sender, EventArgs e)  
  4. {  
  5.     Service1 oService1 = new Service1();  
  6.    
  7.     if (Request["oauth_token"] == null)  
  8.     {  
  9.         WebApplication1.localhost.OAuthTokenResponse oaToken = (WebApplication1.localhost.OAuthTokenResponse)oService1.function1(Request.Url.AbsoluteUri);  
  10.         HttpContext.Current.Response.Redirect(string.Format("http://twitter.com/oauth/authorize?oauth_token={0}", oaToken.Token));  
  11.     }  
  12.     else  
  13.     {  
  14.         string requestToken = Request["oauth_token"].ToString();  
  15.         string pin = Request["oauth_verifier"].ToString();  
  16.         bool i = oService1.function2(requestToken, pin);  
  17.         Response.Write("Message Posted ");  
  18.     }  
Step 5
 
It will ask for a Twitterizer reference; for that use the following procedure:
  • Go to Tools
  • Select "Library Package Manager"
  • Select the Package Manager console then select the source as a Nugget official package source and select your project name in the default project
  • Execute command
  • Install-Package twitterizer -Version 2.3.1
Step 6
 
Rebuild all and see the output.
 
message-in-twitter.jpg
 
message-in-twitter1.jpg


Recommended Free Ebook
Similar Articles