Skip to content

Authentication

linvi edited this page May 11, 2015 · 37 revisions

Overview

Twitter allows developers to authenticate users against their application credentials. The API gives access to 2 different mechanisms that we will name URL redirect authentication and PIN based authentication.

PIN Based authentication is better suited for Desktop application whilst URL redirect authentication is better suited for Web Application.

PIN Based Authentication

The PIN based authentication process is quite simple.

  1. Request Twitter to provide a unique URL that enables a user to authenticate and retrieve a captcha.
  2. Ask the user to go to this URL.
  3. Twitter will ask the user to authenticate and accept the permissions requested by your Twitter application.
  4. If the user accepts, Twitter generates a PIN Code and give it to the user.
  5. With this code, Twitter can now issue a new OAuth Token available from a WebRequest.

Now let's see how Tweetinvi simplifies this process.

// Store the application only credentials into a variable
var applicationCredentials = CredentialsCreator.GenerateApplicationCredentials(consumerKey, consumerSecret);

// Get the URL that the user needs to visit to accept your application
var url = CredentialsCreator.GetAuthorizationURL(applicationCredentials);

// Implement your own method to request the PIN Code from the User
var pinCode = RequestThePinCodeToTheUser();

// Let Tweetinvi generates the credentials based on the given PIN Code
var userCredentials = CredentialsCreator.GetCredentialsFromVerifierCode(pinCode, applicationCredentials);

URL Redirect Authentication

The Redirect URL authentication process is also quite a straightforward process.

  1. Request Twitter to provide a unique URL that enables a user to authenticate and redirect to a specific URL.
  2. Ask the user to go to this URL.
  3. Twitter will ask the user to authenticate and accept the permissions requested by your Twitter application.
  4. If the user accepts, Twitter will redirect the user to the specified URL and provide some credentials information as URL parameters.
  5. With these information, Twitter can now issue a new OAuth Token available from a WebRequest.

Now let's see how Tweetinvi simplifies this process.

  • In a first time we need the user to be redirected to Twitter authentication URL.
// Store the application only credentials into a variable
var applicationCredentials = CredentialsCreator.GenerateApplicationCredentials(consumerKey, consumerSecret);

// Get the URL that the user needs to visit to accept your application
var url = CredentialsCreator.GetAuthorizationURLForCallback(applicationCredentials, "https://mywebsite.com/twitter_auth");
// The callbackURL parameter is the entire URL that you controller received
// The URL will be parsed to retrieve the expected information

var newCredentials = CredentialsCreator.GetCredentialsFromCallbackURL(callbackURL, applicationCredentials);

Please be aware that during the redirect Twitter will provide 2 parameters in the query, oauth_token and oauth_verifier. Please do not use any of these parameters in your callbackURL.

Web Application Considerations

// Credentials Being Null // Localhost

Clone this wiki locally