How to run actions on multiple twitter accounts without setting static TwitterCredentials.SetCredentials

Dec 19, 2014 at 6:52 PM
I don't really understand the use of all the static helper objects, such as
TwitterCredentials.SetCredentials(this.oAuthToken, this.OAuthTokenSecret, this.ApiKey, this.ApiSecret);
From what I understand this sets the credentials, application wide. But I want to allow multiple users to run actions in the application.

So I created an object credentials
var cred = TwitterCredentials.CreateCredentials(this.oAuthToken, this.OAuthTokenSecret, this.ApiKey, this.ApiSecret);
that is used to created an User
var twitterUser = User.GetLoggedUser(this.Credentials);
so far so good, but now when I try and follow another user for example,
var followThisUser = User.GetUserFromScreenName(usernameToFollow);
var success = twitterUser.FollowUser(follow)
I receive an exception, saying I MUST call
var cred = TwitterCredentials.CreateCredentials(this.oAuthToken, this.OAuthTokenSecret, this.ApiKey, this.ApiSecret);
which, if I do, does fix the exception. But I have no set a single users auth data app wide, which I don't want.


Is there anywhere that explains how run actions for multiple users?
Dec 20, 2014 at 12:57 PM
Am still as confused as ever, as the actions don't seemingly require a user

To retweet
 var tweet = Tweet.PublishRetweet(lTweetId); 
return tweet.Retweeted;
I assume it uses the last user who authenticated?

via one of:
var cred = TwitterCredentials.CreateCredentials(this.oAuthToken, this.OAuthTokenSecret, this.ApiKey, this.ApiSecret);

var twitterUser = User.GetLoggedUser(this.Credentials);
But what if I have multiple users logged in?

Must I run the code single threaded,
  • auth a user / set credentials
  • perform action
  • auth another user / set credentials
  • perform action
  • etc etc?
:-s
Editor
Dec 20, 2014 at 1:12 PM
Edited Dec 20, 2014 at 1:13 PM
Hi,

Here's how you do multiple logged in users with TweetInvi:
var credentials = TwitterCredentials.CreateCredentials("Access_Token", "Access_Token_Secret", "Consumer_Key", "Consumer_Secret");
TwitterCredentials.ExecuteOperationWithCredentials(credentials, () =>
{
        Tweet.PublishTweet("myTweet");
});
Ref: http://tweetinvi.codeplex.com/documentation

imlokesh
Dec 20, 2014 at 2:22 PM
ok, so the answer is there on the home page lol. I must have read that page 100 times, doesn't help programming late in to the night.
The credentials have a thread scope, it means that when modifying the credentials in a Thread, it will not affect the other threads. You can use the ApplicationCredentials to change the default credentials of all threads.
so the static objects are all limited to a thread, set the credentials, then anything else that happens on the thread will use the credentitals. Makes sense now :)

Can also utilise lamdas, if not using threads
var credentials = TwitterCredentials.CreateCredentials("Access_Token", "Access_Token_Secret", "Consumer_Key", "Consumer_Secret");
TwitterCredentials.ExecuteOperationWithCredentials(credentials, () =>
{
        Tweet.PublishTweet("myTweet");
});
Marked as answer by user_martin on 12/20/2014 at 7:23 AM
Dec 20, 2014 at 2:23 PM
imlokesh wrote:
Hi,

Here's how you do multiple logged in users with TweetInvi:
var credentials = TwitterCredentials.CreateCredentials("Access_Token", "Access_Token_Secret", "Consumer_Key", "Consumer_Secret");
TwitterCredentials.ExecuteOperationWithCredentials(credentials, () =>
{
        Tweet.PublishTweet("myTweet");
});
Ref: http://tweetinvi.codeplex.com/documentation

imlokesh
Didn't see your reply before post above, but thanks :)