Introduction
Let’s first establish what the purpose of this code is.
For this article, the purpose of the code is How to use Twitter login API with using TweetSharp Library.
Background
Step 1
First of all, we need to create an app in Twitter account because we need ConsumerKey & ConsumerSecret of that app. So, here we go for creating an app in Twitter account.
- Login to developer twitter. Use the below link for the same:
- Click on "Create New App" on top menu.
- Fill all the fields for our new application.
- Go to the "Keys and Access Tokens" tab and get your ConsumerKey & ConsumerSecret.
Using the Code
Step 2
Now it's code time....
- Create an empty MVC application. On the "File" menu, click "New Project".
- In the New Project dialog box, under Project types, expand "Visual C#", and then click "Web". In the Name box, type "
TwitterLoginAPI
", then click on OK.
- Now, in the dialog box, click on "MVC" under the ASP.NET 4.5.2 Templates. Then, click on "Change Authentication" on the center of the right side. Select "No Authentication".
Note: We use TweetSharp
library. So first of all, we need this package.
- Install TweetSharp Library. In "Package Manager Console", type "
Install-Package TweetSharp
" and hit enter.
- First of all, We need to understand Flow of login API in Web Application.
- Obtaining a request token
- Redirecting the user to Twitter Page
- Get Access Tokens
- According to Access Tokens, get user profile details
- Create two Action methods "
Index
" as GET
and "TwitterAuth
" as GET
in your Home Controller.
[HttpGet]
public ActionResult Index()
{
return View();
}
[HttpGet]
public ActionResult TwitterAuth()
{
return View();
}
- Copy the below HTML in your Index.cshtml.
@{
ViewBag.Title = "Twitter";
}
@using (Html.BeginForm("TwitterAuth", "Home", FormMethod.Get))
{
<br />
<button class="btn btn-info" type="submit" id="twitter">Huree! Twitter</button>
}
- Now, obtaining a request token & Redirecting the user to Twitter Page.
[HttpGet]
public ActionResult TwitterAuth()
{
string Key = "Enter your consumer key";
string Secret = "Enter your consumer secret";
TwitterService service = new TwitterService(Key, Secret);
OAuthRequestToken requestToken = service.GetRequestToken
("http://localhost:62630/Home/TwitterCallback");
Uri uri = service.GetAuthenticationUrl(requestToken);
return Redirect(uri.ToString());
}
- Get Access Tokens & according to Access Tokens, get user profile details:
public ActionResult TwitterCallback(string oauth_token, string oauth_verifier)
{
var requestToken = new OAuthRequestToken { Token = oauth_token };
string Key = "Enter your costumer key";
string Secret = "Enter your consumer secret";
try
{
TwitterService service = new TwitterService(Key, Secret);
OAuthAccessToken accessToken =
service.GetAccessToken(requestToken, oauth_verifier);
service.AuthenticateWith(accessToken.Token, accessToken.TokenSecret);
VerifyCredentialsOptions option = new VerifyCredentialsOptions();
TwitterUser user = service.VerifyCredentials(option);
return View();
}
catch
{
throw;
}
}
All done! :)