Tweet using specific credentials

Feb 26, 2014 at 2:29 PM
Edited Feb 26, 2014 at 3:14 PM
Hello,
I would like to know if there's a way to publish a tweet using specific credentials.
In an older release, we had the possibility to specifiy a token.
I understand that now you're using static thread to keep user credentials into seperate threads.
But in an applicaiton that manage multiple accounts, we always need to SetCredentials before publish, otherwise the tweet won't be published on the right account.
Thanks

PS: Is there a way to get the error when publish fails ?
Feb 26, 2014 at 3:28 PM
Edited Feb 27, 2014 at 5:51 PM
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
Feb 27, 2014 at 4:46 PM
Thanks for the reply,

In my application, every tweets are different foreach users, so i don't need to iterate through credentials, i just need to store an instance for each user, and then publish a specific tweet. I already implemented a workaround, but i'm really waiting for the next release :)

I had another question : Is there a way to get the error when publish fails ?
Feb 27, 2014 at 5:51 PM
Oh I see so you selected solution 3?

Is there a way to get the error when publish fails ?

If you want to get an the details of an error occurring you can do the following :
var tweet = Tweet.PublishTweet("pdq");
if (tweet == null)
{
         var exception = ExceptionHandler.GetLastException().WebException;
}
I will improve the management of Exception in 0.9.0.2 in the coming days.
You'll notably have access to an event informing you that an error occurred.
I am still undecided on whether incorrect parameters like a null tweet should be added in the ExceptionHandler. Please let me know what you think.

Linvi
Feb 28, 2014 at 1:32 PM
Thanks for the reply.
by null tweet you mean a tweet without text for exemple ?
It should raise a ArgumentNullException, no ?
Have you planed in the exception management, to retrieve exceptions not from "GetLast", like throwing custom exceptions, or filling the tweet object whit the exception ?
Feb 28, 2014 at 3:24 PM
Hi,

The ExceptionHandler logs all the Web Exceptions during the application lifecycle. Therefore you can access all the Exceptions coming from Twitter.
When implementing 0.9 I wanted to have developers never do Try/Catch to an extent that Tweetinvi never throw any exception in the current version. If arguments are effectively invalid or a webexception is thrown form Twitter, the result will be null.

With the benefit of hindsight I think it was not a wise decision.
For next release (0.9.1) I will try to change this to follow this behavior.

If a WebException is Thrown -> ExceptionHander registers the WebException and the result is null (does not throw exception).
If an Argument is invalid (e.g. null) an exception will be Thrown.

Consequently in next version the user will be able to differentiate a WebException from an exception created because he did some code wrong.

by null tweet you mean a tweet without text for exemple ?
By null I mean the returned result is null.

It should raise a ArgumentNullException, no ?
As I've just explained, it won't but I think it should be.

Have you planed in the exception management, to retrieve exceptions not from "GetLast", like throwing custom exceptions, or filling the tweet object whit the exception ?
As explained, yes you already can do that.

Please feel free to ask any other question concerning this subject. I think Exception management for exception oustide of WebException is not good in the current version and I'll work hard to improve this.

Linvi