Twitter Login API Using TweetSharp In MVC

Let’s first establish what the purpose of this code is. For this article, the purpose of the code is to create Twitter login API using TweetSharp Library in MVC.

Step 1

First of all, we need to create an app in a Twitter account because we need ConsumerKey & ConsumerSecret of that app. So, here we create an app in a Twitter account.

  1. Login to developer Twitter. Use this link for the same.
  2. Click on "Create New App" on top menu.

    twitter

  3. Fill in all the fields for our new application.

    twitter

  4. Go to the "Keys and Access Tokens" tab and get your ConsumerKey & ConsumerSecret.

    twitter

Step 2  Now, it's Code Time

  1. Create an empty MVC application. On the "File" menu, click "New Project".

  2. In the "New Project" dialog box, under Project types, expand "Visual C#", and then click "Web". 

  3. In the Name box, type "TwitterLoginAPI", then click on OK.

  4. 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. 

  5. Select "No Authontication".

    Note - We are using the TweetSharp library, so first of all, we need this package.

  6. Install TweetSharp Library.

    In "Package Manager Console", type "Install-Package TweetSharp" and hit Enter.

    twitter

  7. Now, we need to understand the flow of the login API in web application.

    • Obtain a request token
    • Redirect the user to Twitter Page
    • Get Access Tokens
    • According to Access Tokens, get user profile details

  8. Create two Action Methods - "Index" as GET and "TwitterAuth" as GET in your Home Controller.
    1. [HttpGet]  
    2. public ActionResult Index() {  
    3.         return View();  
    4.     }  
    5.     [HttpGet]  
    6. public ActionResult TwitterAuth() {  
    7.         return View();  
    8.     } // This is just a sample script. Paste your real code (javascript or HTML) here.  
    9. if ('this_is' == /an_example/) {  
    10.     of_beautifier();  
    11. else {  
    12.     var a = b ? (c % d)  e[f];  
    13. }  
  9. Copy the below HTML in your Index.cshtml.
    1. @ {  
    2.     ViewBag.Title = "Twitter";  
    3. }  
    4. @using(Html.BeginForm("TwitterAuth""Home", FormMethod.Get)) { < br / > < button class = "btn btn-info"  
    5.     type = "submit"  
    6.     id = "twitter" > Huree!Twitter < /button>  
    7. }  
  10. Now, obtain a request token & redirect the user to the Twitter page.
    1. [HttpGet]  
    2. public ActionResult TwitterAuth() {  
    3.     string Key = "Enter your cosumer key";  
    4.     string Secret = "Enter your cosumer secret";  
    5.     TwitterService service = new TwitterService(Key, Secret);  
    6.     //Obtaining a request token  
    7.     OAuthRequestToken requestToken = service.GetRequestToken("http//localhost62630/Home/TwitterCallback");  
    8.     Uri uri = service.GetAuthenticationUrl(requestToken);  
    9.     //Redirecting the user to Twitter Page  
    10.     return Redirect(uri.ToString());  
    11. }  
  11. Get Access Tokens and according to that, get user profile details.
    1. public ActionResult TwitterCallback(string oauth_token, string oauth_verifier) {  
    2.     var requestToken = new OAuthRequestToken {  
    3.         Token = oauth_token  
    4.     };  
    5.     string Key = "Enter your cosumer key";  
    6.     string Secret = "Enter your cosumer secret";  
    7.     try {  
    8.         TwitterService service = new TwitterService(Key, Secret);  
    9.         //Get Access Tokens  
    10.         OAuthAccessToken accessToken = service.GetAccessToken(requestToken, oauth_verifier);  
    11.         service.AuthenticateWith(accessToken.Token, accessToken.TokenSecret);  
    12.         VerifyCredentialsOptions option = new VerifyCredentialsOptions();  
    13.         //According to Access Tokens get user profile details  
    14.         TwitterUser user = service.VerifyCredentials(option);  
    15.         return View();  
    16.     } catch {  
    17.         throw;  
    18.     }  
    19. }  

That's it. We have done it all successfully.

Next Recommended Reading Login Page In MVC Using Entity Framework