Twitter API v2 Authorization works only on some endpoints - Unauthorize

26 Views Asked by At

I’m trying to integrate twitter api in a .net core web api app. I got a Basic account. Tried calls in postman and I was able to make them work right, but when I try from C# code it happens that for an endpoint works as expected but for another endpoint doesn’t work.

I’m using the OAuth 1.0.3 nuget package to generate the authorization header and both my calls are managed by the following code:

private RestResponse ExecApiCall(string uri){
var options = new RestClientOptions("https://api.twitter.com")
{
    MaxTimeout = -1,
};

var client = new RestClient(options);
var request = new RestRequest(uri, Method.Get);

var oAuth = new OAuthRequest
{
    Method = "GET",
    Type = OAuthRequestType.ProtectedResource,
    SignatureMethod = OAuthSignatureMethod.HmacSha1,
    ConsumerKey = CONSUMER_KEY,
    ConsumerSecret = CONSUMER_SECRET,
    Token = ACCESS_TOKEN,
    TokenSecret = ACCESS_SECRET,
    RequestUrl = "https://api.twitter.com" + uri
};

string _authHeader = oAuth.GetAuthorizationHeader();

request.AddHeader("Authorization", _authHeader);

RestResponse response = client.Execute(request);

return response;}

With this call works ok: string uri = $“/2/users/{UserId}/liked_tweets”; var res = ExecApiCall(uri);

With this other call I get Unauthorized: string uri = $“/2/tweets/search/recent?query=conversation_id:{TweetId} from:{UserId}”; var res = await ExecApiCall(uri);

Both endpoints works fine with Postman.

Can anybody help? Thank you

0

There are 0 best solutions below