Skip to content

Credentials before Tweetinvi 0.9.9.x

linvi edited this page Aug 2, 2015 · 1 revision

Overview

Twitter executes queries against credentials that are stored as OAuth Tokens. These tokens allow Twitter to identify the Application that wants to execute an operation on behalf of a specific User.

It is important to be aware that each query executed against the Twitter REST API or Stream API requires a WebRequest to contain credentials information in the headers.

The credentials information can be divided into two, the Application Credentials and the User Credentials.

Application Credentials

Application credentials are generated by Twitter on its apps website.

After creating your application, the interface will show you a Keys and Access Tokens which will contain the magical information we are looking for.

  • Consumer Key (API Key)
  • Consumer Secret (API Secret)

User Credentials

When a user wants to use an application, Twitter generates a second pair of credentials that will be specific to a user and an application.

The user credentials contains the 2 following keys:

  • Access Token
  • Access Token Secret

You can get more information on how to generate user credentials information in Authentication.

Credentials and multiple threads

Each thread uses its own set of credentials. When performing a WebRequest from a thread that is not the main thread, the default credentials will be a clone of the TwitterCredentials.ApplicationCredentials property.

Using TwitterCredentials.SetCredentials(); in a thread will not affect the credentials of the other threads.

Let's code

There are 3 ways to use credentials in Tweetinvi.

The most common one is to set up the credentials globally. When set up globally, any method invoked by the developer will use these specific credentials (PS: LoggedUser operations are a special case, please read below to know more).

// Applies credentials for the current thread. If used for the first time, set up the ApplicationCredentials
TwitterCredentials.SetCredentials("Access_Token", "Access_Token_Secret", "Consumer_Key", "Consumer_Secret");

// When a new thread is created, the default credentials will be the Application Credentials
TwitterCredentials.ApplicationCredentials = TwitterCredentials.CreateCredentials("Access_Token", "Access_Token_Secret", "Consumer_Key", "Consumer_Secret");

A second way to use credentials in Tweetinvi is to use the ExecuteOperationWithCredentials method.

// Generate credentials that we want to use
var creds = TwitterCredentials.CreateCredentials("Access_Token", "Access_Token_Secret", "Consumer_Key", "Consumer_Secret");

// Use the ExecuteOperationWithCredentials method to invoke an operation with a specific set of credentials
var tweet = TwitterCredentials.ExecuteOperationWithCredentials(creds, () =>
{
      return Tweet.PublishTweet("Hello World");
});

The last solution is to use the LoggedUser. Logged user operations are pretty restricted but they will always perform actions with their original credentials.

// Get the ILoggedUser from the specified credentials
var loggedUser = User.GetLoggedUser(creds);

// Perform an operation with creds - this is true even if the thread uses another set of credentials
loggedUser.GetFriends();

Application-Only Credentials

Tweetinvi does not yet support Application-Only credentials. This feature should be introduced in Tweetinvi 0.9.9.0.

Thank you for your understanding.

Get Credentials from Twitter Website

For testing purposes, the apps website gives you the possibility to generate such credentials. The generated credentials will be related to the User who owns the application.

<< PREVIOUS: Basics --- NEXT : Authentication >>

Clone this wiki locally