Quantcast
Channel: Tweetinvi a friendly Twitter C# library
Viewing all articles
Browse latest Browse all 4126

New Post: Tweet using specific credentials

$
0
0
Hi there,

Just so you know there is a Work Item related with the ability to execute an action over multiple accounts.
This will be implemented in next release.

In the meantime here are 3 different solutions for this "problem".
I would strongly advise to use solution number 2.
List<IOAuthCredentials> credentials = new List<IOAuthCredentials>();
Solution 1 : Update Store Credentials
Previously you must have store the Token is some sort of collection.
Like before just iterate over your list of credentials and set the current credentials before performing your action.
foreach (var credential in credentials)
{
    TwitterCredentials.Credentials = credential;
    Tweet.PublishTweet("My Tweet");
}
Solution 2 : Use Exstension method
Simplify your life and create an Extension method for credentials:
public static class CredentialsExtensions
{
    public static T ExecuteOperation<T>(this IOAuthCredentials credentials, Func<T> operation)
    {
        var initialCredentials = TwitterCredentials.Credentials;
        TwitterCredentials.Credentials = credentials;
        var result = operation();
        TwitterCredentials.Credentials = initialCredentials;
        return result;
    }

    public static void ExecuteOperation(this IOAuthCredentials credentials, Action operation)
    {
        var initialCredentials = TwitterCredentials.Credentials;
        TwitterCredentials.Credentials = credentials;
        operation();
        TwitterCredentials.Credentials = initialCredentials;
    }
}
Then iterate again like this:
foreach (var credential in credentials)
{
    credential.ExecuteOperation(() => Tweet.PublishTweet("My Tweet"));
}
Solution 3: Use a list of ILoggedUsers
var loggedUsers = new List<ILoggedUser>();
foreach (var loggedUser in loggedUsers)
{
    loggedUser.PublishTweet("piloupe!");
}
The issue with this solution is that I have not provided any method to generate a LoggedUser from a set of credentials. It means that to create your list, you will have to iterate over your stored credentials set them and get User.GetLoggedUser().

Please let me know if this solves your problem.
Linvi

Viewing all articles
Browse latest Browse all 4126

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>