Integrate Twitter API in Asp.net

04 / Feb / 2016 by Mohit Kumar 0 comments

Introduction

In this Blog I will explain how to integrate Twitter API with C# Asp.net.

Before Going to start twitter API integration we should do following steps:

– You should have twitter account.

1) Authorize your account in twitter and open https://apps.twitter.com/,create new app button

twitter_1

2) Provide application name should be unique, description and website URL can be dummy:

Twitter_2

3) Scroll bottom, accept checkbox and click create your twitter application

Twitter_3

4) Your app created, go keys and access tokens tab

Twitter_4

5) Copy the API key and API secret to clipboard

Twitter_5

6) Paste them to notepad in this form

Twitter_6

This string should be base64-encoded I using https://www.base64encode.org/ with UTF-8

Encoded string:

Twitter_7

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

Using the Code in ASP.NET

Note:- Do not forgot twitter anywhere supports https requests only not http.

To get bearer access token send http post request and pass credentials to Authorization header

string credentials =”bDZuekx3cVNCTTV3Y3hNYTE4WTY5Vlh6WTpzMXJ3UTNxb1pZWGl3TzJCWUpsT25LSG5aSlZCU09nQUdLOVpQblRjSHdPMnJ5MXhhRA==”;

string access_token = “”;

var post = WebRequest.Create(“https://api.twitter.com/oauth2/token”) as HttpWebRequest;

post.Method = “POST”;

post.ContentType = “application/x-www-form-urlencoded”;

post.Headers[HttpRequestHeader.Authorization] = “Basic ” + credentials;

var reqbody = Encoding.UTF8.GetBytes(“grant_type=client_credentials”);

post.ContentLength = reqbody.Length;

using (var req = post.GetRequestStream())

{

req.Write(reqbody, 0, reqbody.Length);

}

try

{

string respbody = null;

using (var resp = post.GetResponse().GetResponseStream())//there request sends

{

var respR = new StreamReader(resp);

respbody = respR.ReadToEnd();

}

 

//TODO use a library to parse json

access_token = respbody.Substring(respbody.IndexOf(“access_token\”:\””) + “access_token\”:\””.Length, respbody.IndexOf(“\”}”) – (respbody.IndexOf(“access_token\”:\””) + “access_token\”:\””.Length));

}

catch //if credentials are not valid (403 error)

{

//TODO

}

//rest api using

var gettimeline = WebRequest.Create(“https://api.twitter.com/1.1/statuses/user_timeline.json?count=3&screen_name=twitterapi”) as HttpWebRequest;

gettimeline.Method = “GET”;

gettimeline.Headers[HttpRequestHeader.Authorization] = “Bearer ” + access_token;

try

{

string respbody = null;

using (var resp = gettimeline.GetResponse().GetResponseStream())//there request sends

{

var respR = new StreamReader(resp);

respbody = respR.ReadToEnd();

}

//TODO use a library to parse json

Response.Write(respbody);

}

catch //401 (access token invalid or expired)

{

//TODO

}

//invalidating un necessary access token

var inv = WebRequest.Create(“https://api.twitter.com/oauth2/invalidate_token”) as HttpWebRequest;

inv.Method = “POST”;

inv.ContentType = “application/x-www-form-urlencoded”;

inv.Headers[HttpRequestHeader.Authorization] = “Basic ” + credentials;

var reqbodyinv = Encoding.UTF8.GetBytes(“access_token=” + access_token);

inv.ContentLength = reqbodyinv.Length;

using (var req = inv.GetRequestStream())

{

req.Write(reqbodyinv, 0, reqbodyinv.Length);

}

try

{

post.GetResponse();

}

catch //token not invalidated

{

//TODO

}

Results

Twitter_8

In addition you can read more about ASP.NET web forms application

FOUND THIS USEFUL? SHARE IT

Leave a Reply

Your email address will not be published. Required fields are marked *