I am the main developer of Tweetinvi.
PLEASE READ THIS POST ENTIRELY so that you have a good understanding of the subject.
Tweetinvi Auth
class is storing the credentials as a per thread static variable.
What this mean is that for every Thread in which your application store its own set of credentials. In ASP.NET this mean that every thread from the ThreadPool will use their own set of credentials.
This also mean that each time you create a new Thread it is preferable to manually set the credentials for this thread using Auth.SetUserCredentials
To answer your question you have different solutions:
Send Tweet in same Thread
You just have to invoke Auth.SetUserCredentials
every time you want to switch user.
// creds for user 1
Auth.SetUserCredentials("CONSUMER_KEY_1", "CONSUMER_SECRET_1", "ACCESS_TOKEN_1", "ACCESS_TOKEN_SECRET_1");
var tweetPublishedByUser1 = Tweet.PublishTweet("user 1 tweet");
// creds for user 2
Auth.SetUserCredentials("CONSUMER_KEY_2", "CONSUMER_SECRET_2", "ACCESS_TOKEN_2", "ACCESS_TOKEN_SECRET_2");
var tweetPublishedByUser2 = Tweet.PublishTweet("user 2 tweet");
Create Credentials and execute operation with it
You can ask Tweetinvi to run any operation with a specific set of credentials using Auth.ExecuteOperationWithCredentials
.
// creds for user 1
var cred1 = Auth.CreateCredentials("CONSUMER_KEY_1", "CONSUMER_SECRET_1", "ACCESS_TOKEN_1", "ACCESS_TOKEN_SECRET_1");
var tweetPublishedByUser1 = Auth.ExecuteOperationWithCredentials(cred1, () =>
{
return Tweet.PublishTweet("user 1 tweet");
});
// creds for user 2
var cred2 = Auth.CreateCredentials("CONSUMER_KEY_2", "CONSUMER_SECRET_2", "ACCESS_TOKEN_2", "ACCESS_TOKEN_SECRET_2");
var tweetPublishedByUser2 = Auth.ExecuteOperationWithCredentials(cred2, () =>
{
return Tweet.PublishTweet("user 2 tweet");
});
AuthenticatedUser
The AuthenticatedUser
simplify your life via the ExecuteAuthenticatedUserOperation
method. This solution is quite useful as it allows you to create a collection of AuthenticatedUser
and use the one you want when you want.
// creds for user 1
Auth.SetUserCredentials("CONSUMER_KEY_1", "CONSUMER_SECRET_1", "ACCESS_TOKEN_1", "ACCESS_TOKEN_SECRET_1");
var authenticatedUser1 = User.GetAuthenticatedUser();
// creds for user 2
Auth.SetUserCredentials("CONSUMER_KEY_2", "CONSUMER_SECRET_2", "ACCESS_TOKEN_2", "ACCESS_TOKEN_SECRET_2");
var authenticatedUser2 = User.GetAuthenticatedUser();
var tweetPublishedByUser1 = authenticatedUser1.ExecuteAuthenticatedUserOperation(() =>
{
return Tweet.PublishTweet("user 1 tweet");
});
var tweetPublishedByUser2 = authenticatedUser2.ExecuteAuthenticatedUserOperation(() =>
{
return Tweet.PublishTweet("user 2 tweet");
});
References
You can find the documentation of how to use credentials in Tweetinvin here : https://github.com/linvi/tweetinvi/wiki/Credentials