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.
- Login to developer Twitter. Use this link for the same.
- Click on "Create New App" on top menu.
- Fill in all the fields for our new application.
- Go to the "Keys and Access Tokens" tab and get your ConsumerKey & ConsumerSecret.
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 Authontication".
Note - We are using the TweetSharp library, so first of all, we need this package.
- Install TweetSharp Library.
In "Package Manager Console", type "Install-Package TweetSharp" and hit Enter.
- 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
- 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();
- }
- if ('this_is' == /an_example/) {
- of_beautifier();
- } else {
- var a = b ? (c % d) e[f];
- }
- 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, obtain a request token & redirect the user to the Twitter page.
- [HttpGet]
- public ActionResult TwitterAuth() {
- string Key = "Enter your cosumer key";
- string Secret = "Enter your cosumer secret";
- TwitterService service = new TwitterService(Key, Secret);
-
- OAuthRequestToken requestToken = service.GetRequestToken("http//localhost62630/Home/TwitterCallback");
- Uri uri = service.GetAuthenticationUrl(requestToken);
-
- return Redirect(uri.ToString());
- }
- Get Access Tokens and according to that, get user profile details.
- public ActionResult TwitterCallback(string oauth_token, string oauth_verifier) {
- var requestToken = new OAuthRequestToken {
- Token = oauth_token
- };
- string Key = "Enter your cosumer key";
- string Secret = "Enter your cosumer 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;
- }
- }
That's it. We have done it all successfully.