Following the previous post “Using POST /2/tweets in C#, I simply can’t do it” I’m experiencing the same problems posting tweets from c# code using v2 api.

I’ve collected several methods found on Internet, but none of them works: check github for /Bobetti/TwitterApi (can’t insert link into the post)

All I get is Forbidden or Not Authorized.

Maybe the problem is not in the code, but some wrong settings for the dev application?

I’m guessing that the code you have in there for generating the OAuth signature is not valid somehow, it has been a while since I wrote any .NET code, but I did have something that used to work for reading data - I never tried a POST, and I have a feeling that the OAuth library that I was using may not be supported in the latest .NET version, I need to revisit this.

Have you looked at e.g. LinqToTwitter or another .NET library that does the OAuth piece for you?

I had problems originally using the method you are using to write the contents of the json to the endpoint - using (HttpWebRequest)WebRequest.Create() - this didn’t work for me and I got a very unhelpful not authorized message.

I’m now using a very old version of RestSharp library NuGet Gallery | RestSharp 104.0.0 and note the 104.0.0 version.

The reason is that I got this working originally with the Postman Twitter API v2 (Twitter API v2), and when I put in my keys and did a test POST it worked, and it also allows you to generate C# RestSharp code for the same request, for me it looked like this:

var client = new RestClient("https://api.twitter.com/2/tweets");
client.Timeout = -1;
var request = new RestRequest(Method.POST);
request.AddHeader("Authorization", auth);
request.AddHeader("Content-Type", "application/json");
request.AddHeader("Cookie", "guest_id=v1%3A164647635225822612");
var body = @"{" + "\n" +
@"    ""text"": ""Hello World!""" + "\n" +
@"}";
request.AddParameter("application/json", body,  ParameterType.RequestBody);
IRestResponse response = client.Execute(request);
Console.WriteLine(response.Content);

The new version of RestSharp however doesn’t work with this, but that old 104.0.0 version does. When I used this code to make my request with the same auth header it worked!

Maybe worth a shot?

For completeness, the method I used to get the header is from GitHub - rhargreaves/oauth-dotnetcore: An implementation of OAuth 1.0a for .NET Core based on Daniel Crenna’s vaulted OAuth library

OAuthRequest oAclient = OAuthRequest.ForProtectedResource("POST", CONSUMER_KEY, CONSUMER_SECRET, ACCESS_TOKEN, ACCESS_TOKEN_SECRET);
oAclient.RequestUrl = "https://api.twitter.com/2/tweets";
string auth = oAclient.GetAuthorizationHeader();

Also, for settings in the developer portal, I assume you enabled the correct version of oAuth, and that you have oauth 1.0a settings / Read and write Read and Post Tweets and profile information selected, and that you have URLs in the Callback URI / Redirect URL, and the Website URL (even if they are blank webpages I think you may still need to have them entered and valid?).

I recommend the Postman API addon first to confirm your keys are all good - you might need to clone it to enter your keys to get it working - if that POSTs with your keys then you know it’s your code and not your keys! Note that I couldn’t get Postman working with the same keys without the add-on v2 API workspace!

If we will step back from the code and head over to Postman, here is what I get from postman

dev app settings:

Yes I’ve added callback urls - but for now was testing from console app

Hmm, the same everywhere! Only difference is
image

vs
image

No ?text=Hello, but rather in the body/raw section.

The rest of the settings the same?

We have a different number of headers (9 vs 11)

Here are my headers:

Have you tried regenerating all your keys?

1 Like

Ralph, you’re a genius!!! Now it is working, finally. 4 days spent - ohhh

3 Likes

HURRAY! So glad it’s working :smiley:

1 Like