Deepesh
Deepesh

Reputation: 11

Posting Image to Twitter using c# windows application

I have tried the same with a web application, it is working. But while I am doing the same in c# windows application the response given is

Unable to publish to Twitter.

async Task<Tuple<int, string>> SendImage(string URL, MultipartFormDataContent multipartContent)
{
    using (var httpClient = new HttpClient())
    {
        httpClient.DefaultRequestHeaders.Add("Authorization", PrepareOAuth(URL, null, "POST"));

        var httpResponse = await httpClient.PostAsync(URL, multipartContent);
        var httpContent = await httpResponse.Content.ReadAsStringAsync();

        return new Tuple<int, string>(
            (int)httpResponse.StatusCode,
            httpContent
        );
    }
}

On httpResponse code I get below shown error.

Unknown error publishing to TwitterError

Upvotes: 1

Views: 498

Answers (1)

Linvi
Linvi

Reputation: 2087

Have you thought of using a library like Tweetinvi. Here is how to both upload the photo and publish the tweet.

var credentials = new TwitterCredentials("CONSUMER_KEY", "CONSUMER_SECRET", "ACCESS_TOKEN", "ACCESS_TOKEN_SECRET");
var client = new TwitterClient(credentials);

var photo = File.ReadAllBytes("myfile_path/photo.jpg")
var uploadedPhoto = await client.Upload.UploadTweetImageAsync(photo);
var tweet = await client.Tweets.PublishTweetAsync(new PublishTweetParameters("hello")
{
    Medias = {uploadedPhoto}
});

Documentation for upload

Upvotes: 1

Related Questions