Click here to Skip to main content
15,878,959 members
Articles / Web Development / HTML
Tip/Trick

Twitter Login API with Using TweetSharp Library

Rate me:
Please Sign up or sign in to vote.
3.41/5 (6 votes)
27 Dec 2016CPOL2 min read 14.2K   282   5   4
Twitter Login API using TweetSharp Library

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.

  1. Login to developer twitter. Use the below link for the same:
  2. Click on "Create New App" on top menu.

    Image 1

  3. Fill all the fields for our new application.

    Image 2

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

    Image 3

Using the Code

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". In the Name box, type "TwitterLoginAPI", then click on OK.
  3. 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.

  4. Install TweetSharp Library. In "Package Manager Console", type "Install-Package TweetSharp" and hit enter.

    Image 4

  5. 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
  6. Create two Action methods "Index" as GET and "TwitterAuth" as GET in your Home Controller.
    C#
    [HttpGet]
    public ActionResult Index()
    {
       return View();
    }
    
    [HttpGet]
    public ActionResult TwitterAuth()
    {
       return View();
    }
  7. Copy the below HTML in your Index.cshtml.
    HTML
    @{
        ViewBag.Title = "Twitter";
    }
    
    @using (Html.BeginForm("TwitterAuth", "Home", FormMethod.Get))
    {
        <br />
        <button class="btn btn-info" type="submit" id="twitter">Huree! Twitter</button>
    }
  8. Now, obtaining a request token & Redirecting the user to Twitter Page.
    C#
    [HttpGet]
    public ActionResult TwitterAuth()
    {
                string Key = "Enter your consumer key";
                string Secret = "Enter your consumer secret";
    
                TwitterService service = new TwitterService(Key, Secret);
    
                //Obtaining a request token
                OAuthRequestToken requestToken = service.GetRequestToken
                                                 ("http://localhost:62630/Home/TwitterCallback");
    
                Uri uri = service.GetAuthenticationUrl(requestToken);
    
                //Redirecting the user to Twitter Page
                return Redirect(uri.ToString());
    }
  9. Get Access Tokens & according to Access Tokens, get user profile details:
    C#
    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);
    
                    //Get Access Tokens
                    OAuthAccessToken accessToken = 
                               service.GetAccessToken(requestToken, oauth_verifier);
    
                    service.AuthenticateWith(accessToken.Token, accessToken.TokenSecret);
    
                    VerifyCredentialsOptions option = new VerifyCredentialsOptions();
    
                    //According to Access Tokens get user profile details
                    TwitterUser user = service.VerifyCredentials(option);
    
                    return View();
                }
                catch
                {
                    throw;
                }            
    }

All done! :)

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer (Senior) Riowebs
India India
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
QuestionWhy? Pin
Matthew C Schmidt28-Dec-16 9:15
professionalMatthew C Schmidt28-Dec-16 9:15 
AnswerRe: Why? Pin
silicon_ghost4-Jan-17 2:28
silicon_ghost4-Jan-17 2:28 
GeneralRe: Why? Pin
Matthew C Schmidt4-Jan-17 5:19
professionalMatthew C Schmidt4-Jan-17 5:19 
GeneralMy vote of 4 Pin
Gedarius28-Dec-16 7:24
Gedarius28-Dec-16 7:24 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.