2

We are developing a social media monitoring application in ASP.NET (C# - Framework 4.0) and using the Tweetinvi library for Twitter.

We must send tweets and search for various Twitter users at the same time. In order to publish tweets, we must firstly set the access tokens in Auth class. But, all functions there are static. If we set any users' tokens, all other users publish his tweets with this account.

How can I prevent this problem?

Dale K
  • 25,246
  • 15
  • 42
  • 71
DemirMengu
  • 51
  • 3

1 Answers1

2

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

Linvi
  • 2,077
  • 1
  • 14
  • 28
  • When it runs in localhost it works correctly but in web, asp.net session ends and it does not work. Why it should be like this? – DemirMengu Oct 06 '16 at 20:36
  • Hello there. Sorry but I do not understand your question. Also if this fixed your problem please mark it as resolved for other members of stackoverflow who are trying to solve the same problem as you are. – Linvi Oct 07 '16 at 07:59