| | | |

Social Opinion API Support for V2 Post Xeet? (Tweet) Now Available

For historical reasons, the .NET open-source developer SDK I created and maintain only had support for the X v1.1 endpoints when posting a status (Tweet? Xeet?).

 

A few from the community encountered issues when trying to use earlier versions of the SDK to post content using V2 methods.

~

The Problem

After much debugging, I tracked the issue down to a couple of things:

  • OAuth signature in POST requests that use a JSON body.
  • Change at Twitter API end handling POST requests

 

Despite using valid API keys and consumer secrets, the issues remained.  You can read more about this in an issue raised on Github.

~

 

The Solution

The SDK uses a hand rolled OAuth mechanism to generate the required signature and I despite debugging and validating each field, I couldn’t see what was wrong with any of the parameters.

 

I added OAuth.DotNetCore and REST Sharp as NuGet packages to the project for this endpoint, and what do you know, it worked.

 

Additionally, unlike before, where you could just pass a string parameter for your status text, the V2 endpoint requires you to serialize a JSON object and pass it in the body of the request.

 

For example:

PostTextOnyTweetDTO postTweet = new PostTextOnyTweetDTO { text = tweetText };

string tweetToPost = JsonConvert.SerializeObject(postTweet);

string jsonResponse = client.PostTweetV2(tweetToPost);

 

Posting content with images requires you to create an array of said images.  In reality you need to consume a different endpoint to generate image_ids.

~

Using the New Method

In the updated SDK, you can post content onto X/Twitter using just a few lines of code.

 

First, get your keys and tokens from the developer portal:

string _ConsumerKey = ConfigurationManager.AppSettings.Get("ConsumerKey");
string _ConsumerSecret = ConfigurationManager.AppSettings.Get("ConsumerSecret");
string _AccessToken = ConfigurationManager.AppSettings.Get("AccessToken");
string _AccessTokenSecret = ConfigurationManager.AppSettings.Get("AccessTokenSecret");

 OAuthInfo oAuthInfo = new OAuthInfo
 {
     AccessSecret = _AccessTokenSecret,
     AccessToken = _AccessToken,
     ConsumerSecret = _ConsumerSecret,
     ConsumerKey = _ConsumerKey

 };

 

Next, instantiate the TweetService class and call PostTextOnlyTweet

 TweetService tweetsService = new TweetService(oAuthInfo);

 var newTweetModel2 = tweetsService.PostTextOnlyTweet("test 123");

 

Examining the debugger shows the request is successful:

 

Checking the X/Twitter associated with the API keys shows the content has been posted:

~

Summary

This issue has been around for a few months now and I’ve only just got round to resolving it.

 

Shout out to  Gary Ewan Park (@gep13) / X (twitter.com) and Andrew Gubskiy (@andrew_gubskiy) / X (twitter.com) for raising the issue, contributing, and sharing what they found and know.

~

Further Reading and Resources

  • GitHub Repo – full source code for the SDK is available here.
  • NuGet Package – NuGet Package for the SDK is available here.
  • X Developer Platform – developer API reference here.
JOIN MY EXCLUSIVE EMAIL LIST
Get the latest content and code from the blog posts!
I respect your privacy. No spam. Ever.

Similar Posts

Leave a Reply