Dan Cook
Dan Cook

Reputation: 11

Why are these tweets failing to publish using Tweetinvi?

I'm trying to publish a tweet using the API and the Tweetinvi library by following this tutorial.

When the code runs, neither of the tweets publish. Can you offer any suggestions to get them to publish?

static void Main(string[] args)
{
    Auth.SetUserCredentials("1", "2", "3", "4");
    var user = User.GetAuthenticatedUser();


    Console.WriteLine(user);
    var tweet = Tweet.PublishTweet("Hello from 2D_Racer");
    var tweet2 = Tweet.PublishTweet("Hello World");

    var timeLineTweets = Timeline.GetUserTimeline(user, 5);
    foreach (var timeLineTweet in timeLineTweets)
        Console.WriteLine(timeLineTweet);

    Console.ReadLine();
}

Upvotes: 1

Views: 430

Answers (1)

Timothy G.
Timothy G.

Reputation: 9185

The video you link to uses a version of Tweetinvi that is outdated. You should upgrade to the latest Tweetinvi version. Using said version, you can publish a tweet by first getting a TwitterClient configured for a user, and then publish your tweets. For the timeline portion, you can use GetUserTimelineAsync.

string userName = "SomeUsername";
TwitterClient userClient = new TwitterClient("CONSUMER_KEY", "CONSUMER_SECRET", "ACCESS_TOKEN", "ACCESS_TOKEN_SECRET");
var publishedTweetOne = await userClient.Tweets.PublishTweetAsync("Your first Tweets message.");
var publishedTweetTwo = await userClient.Tweets.PublishTweetAsync("Your second Tweets message.");

var timeLine = await userClient.Timelines.GetUserTimelineAsync(userName);

foreach (var tweet in timeLine)
{
    Console.WriteLine(tweet.Text);
}

One thing to note, you need to have the correct permissions set for your application via the Twitter developer portal. If you do not have proper permissions, you will get "Authorization Required" TwitterExceptions when trying to publish your tweets (this could be what is happening to you, thus verifying this is a good idea).

app Permissions

Additionally, if you have to change your application's permission level, all of its user tokens will be revoked, and you will need to issue new ones and update your code appropriately:

If a permission level is changed, any user tokens already issued to that Twitter app must be discarded and users must re-authorize the App in order for the token to inherit the updated permissions.

Upvotes: 1

Related Questions