-
Notifications
You must be signed in to change notification settings - Fork 221
Authentication
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.
The PIN based authentication process is quite simple.
- Request Twitter to provide a unique URL that enables a user to authenticate and retrieve a captcha.
- Ask the user to go to this URL.
- Twitter will ask the user to authenticate and accept the permissions requested by your Twitter application.
- If the user accepts, Twitter generates a PIN Code and give it to the user.
- 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);
The Redirect URL authentication process is also quite a straightforward process.
- Request Twitter to provide a unique URL that enables a user to authenticate and redirect to a specific URL.
- Ask the user to go to this URL.
- Twitter will ask the user to authenticate and accept the permissions requested by your Twitter application.
- If the user accepts, Twitter will redirect the user to the specified URL and provide some credentials information as URL parameters.
- 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");
- In a second time we need to get the information back from Twitter. After being authenticated, Twitter will redirect the user to a URL with the following format : https://mywebsite.com/twitter_auth?oauth_token={tokenValue}&oauth_verifier={verifierValue}. You will need to create a route in your controller to listen to /twitter_auth. In this route handler you can use the following code.
// 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.
// Credentials Being Null // Localhost