-
Notifications
You must be signed in to change notification settings - Fork 221
Credentials
Twitter execute queries against credentials that are stored as OAuth Tokens. These tokens allow Twitter to identify the Application that want 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 2, the Application Credentials and the User 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 contains the magical information we are looking for.
- Consumer Key (API Key)
- Consumer Secret (API Secret)
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.
There are 3 ways to use credentials in Tweetinvi.
The most common one is to set up the credentials globally. When setup globally any method invoked by the developer will use these specific credentials (PS: LoggedUser operation is a special case, please read below to know more).
// Applies credentials for the current thread. If used for the first time setup 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 operation 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();
Tweetinvi does not yet support Application Only credentials. This feature should be introduced in Tweetinvi 0.9.9.0.
Thank you for your understanding.
For testing purposes, the apps website give you the possibility to generate such credentials. The generated credentials will be related to the User who owns the application.