Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

Twitter Login API with Using TweetSharp Library

0.00/5 (No votes)
27 Dec 2016 1  
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.

  3. Fill all the fields for our new application.

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

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.

  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.
    [HttpGet]
    public ActionResult Index()
    {
       return View();
    }
    
    [HttpGet]
    public ActionResult TwitterAuth()
    {
       return View();
    }
  7. 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>
    }
  8. 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);
    
                //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:
    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 has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here