{"id":32197,"date":"2016-02-04T15:53:28","date_gmt":"2016-02-04T10:23:28","guid":{"rendered":"http:\/\/www.tothenew.com\/blog\/?p=32197"},"modified":"2016-02-09T11:07:53","modified_gmt":"2016-02-09T05:37:53","slug":"integrate-twitter-api-in-asp-net","status":"publish","type":"post","link":"https:\/\/www.tothenew.com\/blog\/integrate-twitter-api-in-asp-net\/","title":{"rendered":"Integrate Twitter API in Asp.net"},"content":{"rendered":"<p><strong>Introduction<\/strong><\/p>\n<p>In this Blog I will explain how to integrate Twitter API\u00a0with C# Asp.net.<\/p>\n<p><strong>Before Going to start twitter API integration we should do\u00a0following\u00a0steps:<\/strong><\/p>\n<p>&#8211; You should have twitter account.<\/p>\n<p><strong>1)<\/strong> Authorize your account in twitter and open\u00a0https:\/\/apps.twitter.com\/,create new app button<\/p>\n<p><img decoding=\"async\" loading=\"lazy\" class=\"alignnone size-full wp-image-32189\" src=\"\/blog\/wp-ttn-blog\/uploads\/2016\/02\/twitter_1.png\" alt=\"twitter_1\" width=\"375\" height=\"179\" \/><\/p>\n<p><strong>2)<\/strong> Provide application name should be unique, description and website URL can be dummy:<\/p>\n<p><img decoding=\"async\" loading=\"lazy\" class=\"alignnone size-full wp-image-32190\" src=\"\/blog\/wp-ttn-blog\/uploads\/2016\/02\/Twitter_2.png\" alt=\"Twitter_2\" width=\"480\" height=\"255\" \/><\/p>\n<p><strong>3)<\/strong> Scroll bottom, accept checkbox and click create your twitter application<\/p>\n<p><img decoding=\"async\" loading=\"lazy\" class=\"alignnone size-full wp-image-32191\" src=\"\/blog\/wp-ttn-blog\/uploads\/2016\/02\/Twitter_3.png\" alt=\"Twitter_3\" width=\"624\" height=\"300\" \/><\/p>\n<p><strong>4)<\/strong> Your app created, go keys and access tokens tab<\/p>\n<p><img decoding=\"async\" loading=\"lazy\" class=\"alignnone size-full wp-image-32192\" src=\"\/blog\/wp-ttn-blog\/uploads\/2016\/02\/Twitter_4.png\" alt=\"Twitter_4\" width=\"524\" height=\"279\" \/><\/p>\n<p><strong>5)<\/strong> Copy the API key and API secret to clipboard<\/p>\n<p><img decoding=\"async\" loading=\"lazy\" class=\"alignnone size-full wp-image-32193\" src=\"\/blog\/wp-ttn-blog\/uploads\/2016\/02\/Twitter_5.png\" alt=\"Twitter_5\" width=\"557\" height=\"261\" \/><\/p>\n<p><strong>6)<\/strong> Paste them to notepad in this form<\/p>\n<p><img decoding=\"async\" loading=\"lazy\" class=\"alignnone size-full wp-image-32194\" src=\"\/blog\/wp-ttn-blog\/uploads\/2016\/02\/Twitter_6.png\" alt=\"Twitter_6\" width=\"624\" height=\"102\" \/><\/p>\n<p>This string should be base64-encoded I using\u00a0https:\/\/www.base64encode.org\/\u00a0with UTF-8<\/p>\n<p>Encoded string:<\/p>\n<p><img decoding=\"async\" loading=\"lazy\" class=\"alignnone size-full wp-image-32195\" src=\"\/blog\/wp-ttn-blog\/uploads\/2016\/02\/Twitter_7.png\" alt=\"Twitter_7\" width=\"624\" height=\"102\" \/><\/p>\n<p style=\"background: white\">Encoded string is credentials to get bearer access token for application. Access token should be get programmatically on every application run and should not store anywhere<\/p>\n<h2>Using the Code in ASP.NET<\/h2>\n<p><strong>Note:-<\/strong> Do not forgot twitter anywhere supports https requests only not http.<\/p>\n<p>To get bearer access token send http post request and pass credentials to Authorization header<\/p>\n<p style=\"text-align: left\">string\u00a0credentials =&#8221;bDZuekx3cVNCTTV3Y3hNYTE4WTY5Vlh6WTpzMXJ3UTNxb1pZWGl3TzJCWUpsT25LSG5aSlZCU09nQUdLOVpQblRjSHdPMnJ5MXhhRA==&#8221;;<\/p>\n<p>string access_token = &#8220;&#8221;;<\/p>\n<p>var post = WebRequest.Create(&#8220;https:\/\/api.twitter.com\/oauth2\/token&#8221;) as HttpWebRequest;<\/p>\n<p>post.Method = &#8220;POST&#8221;;<\/p>\n<p>post.ContentType = &#8220;application\/x-www-form-urlencoded&#8221;;<\/p>\n<p>post.Headers[HttpRequestHeader.Authorization] = &#8220;Basic &#8221; + credentials;<\/p>\n<p>var reqbody = Encoding.UTF8.GetBytes(&#8220;grant_type=client_credentials&#8221;);<\/p>\n<p>post.ContentLength = reqbody.Length;<\/p>\n<p>using (var req = post.GetRequestStream())<\/p>\n<p>{<\/p>\n<p>req.Write(reqbody, 0, reqbody.Length);<\/p>\n<p>}<\/p>\n<p>try<\/p>\n<p>{<\/p>\n<p>string respbody = null;<\/p>\n<p>using (var resp = post.GetResponse().GetResponseStream())\/\/there request sends<\/p>\n<p>{<\/p>\n<p>var respR = new StreamReader(resp);<\/p>\n<p>respbody = respR.ReadToEnd();<\/p>\n<p>}<\/p>\n<p>&nbsp;<\/p>\n<p>\/\/TODO use a library to parse json<\/p>\n<p>access_token = respbody.Substring(respbody.IndexOf(&#8220;access_token\\&#8221;:\\&#8221;&#8221;) + &#8220;access_token\\&#8221;:\\&#8221;&#8221;.Length, respbody.IndexOf(&#8220;\\&#8221;}&#8221;) &#8211; (respbody.IndexOf(&#8220;access_token\\&#8221;:\\&#8221;&#8221;) + &#8220;access_token\\&#8221;:\\&#8221;&#8221;.Length));<\/p>\n<p>}<\/p>\n<p>catch \/\/if credentials are not valid (403 error)<\/p>\n<p>{<\/p>\n<p>\/\/TODO<\/p>\n<p>}<\/p>\n<p>\/\/rest api using<\/p>\n<p>var gettimeline = WebRequest.Create(&#8220;https:\/\/api.twitter.com\/1.1\/statuses\/user_timeline.json?count=3&amp;screen_name=twitterapi&#8221;) as HttpWebRequest;<\/p>\n<p>gettimeline.Method = &#8220;GET&#8221;;<\/p>\n<p>gettimeline.Headers[HttpRequestHeader.Authorization] = &#8220;Bearer &#8221; + access_token;<\/p>\n<p>try<\/p>\n<p>{<\/p>\n<p>string respbody = null;<\/p>\n<p>using (var resp = gettimeline.GetResponse().GetResponseStream())\/\/there request sends<\/p>\n<p>{<\/p>\n<p>var respR = new StreamReader(resp);<\/p>\n<p>respbody = respR.ReadToEnd();<\/p>\n<p>}<\/p>\n<p>\/\/TODO use a library to parse json<\/p>\n<p>Response.Write(respbody);<\/p>\n<p>}<\/p>\n<p>catch \/\/401 (access token invalid or expired)<\/p>\n<p>{<\/p>\n<p>\/\/TODO<\/p>\n<p>}<\/p>\n<p>\/\/invalidating un necessary access token<\/p>\n<p>var inv = WebRequest.Create(&#8220;https:\/\/api.twitter.com\/oauth2\/invalidate_token&#8221;) as HttpWebRequest;<\/p>\n<p>inv.Method = &#8220;POST&#8221;;<\/p>\n<p>inv.ContentType = &#8220;application\/x-www-form-urlencoded&#8221;;<\/p>\n<p>inv.Headers[HttpRequestHeader.Authorization] = &#8220;Basic &#8221; + credentials;<\/p>\n<p>var reqbodyinv = Encoding.UTF8.GetBytes(&#8220;access_token=&#8221; + access_token);<\/p>\n<p>inv.ContentLength = reqbodyinv.Length;<\/p>\n<p>using (var req = inv.GetRequestStream())<\/p>\n<p>{<\/p>\n<p>req.Write(reqbodyinv, 0, reqbodyinv.Length);<\/p>\n<p>}<\/p>\n<p>try<\/p>\n<p>{<\/p>\n<p>post.GetResponse();<\/p>\n<p>}<\/p>\n<p>catch \/\/token not invalidated<\/p>\n<p>{<\/p>\n<p>\/\/TODO<\/p>\n<p>}<\/p>\n<p>Results<\/p>\n<p><img decoding=\"async\" loading=\"lazy\" class=\"alignnone size-full wp-image-32196\" src=\"\/blog\/wp-ttn-blog\/uploads\/2016\/02\/Twitter_8.png\" alt=\"Twitter_8\" width=\"624\" height=\"305\" \/><\/p>\n<p>In addition you can read more about\u00a0<a href=\"http:\/\/www.tothenew.com\/blog\/10-key-basic-things-before-starting-development-on-a-new-asp-net-web-forms-application\/\">ASP.NET web forms application<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Introduction In this Blog I will explain how to integrate Twitter API\u00a0with C# Asp.net. Before Going to start twitter API integration we should do\u00a0following\u00a0steps: &#8211; You should have twitter account. 1) Authorize your account in twitter and open\u00a0https:\/\/apps.twitter.com\/,create new app button 2) Provide application name should be unique, description and website URL can be dummy: [&hellip;]<\/p>\n","protected":false},"author":855,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"iawp_total_views":5},"categories":[1],"tags":[1440,1018,3088,3087,3086],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/32197"}],"collection":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/users\/855"}],"replies":[{"embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/comments?post=32197"}],"version-history":[{"count":0,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/32197\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/media?parent=32197"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/categories?post=32197"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/tags?post=32197"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}