I’m trying to retrieve a Twitter OAuth request token using RestSharp, but no matter what I try the API seems to respond with a 404. Here’s my method code:
public void GetRequestToken()
{
var client = new RestClient("https://api.twitter.com/1");
var authenticator = OAuth1Authenticator.ForRequestToken(_consumerKey, _consumerSecret);
authenticator.ParameterHandling = OAuthParameterHandling.HttpAuthorizationHeader;
client.Authenticator = authenticator;
var request = new RestRequest("/oauth/request_token", Method.POST);
request.AddHeader("oauth_callback", "http%3A%2F%2Fmarkashleybell.com");
var response = client.Execute(request);
var qs = HttpUtility.ParseQueryString(response.Content);
_token = qs["oauth_token"];
_tokenSecret = qs["oauth_token_secret"];
}
_consumerKey and _consumerSecret are retrieved from Web.config settings, and I’ve double-checked that they are correct. _token and _tokenSecret are private member variables of the class containing this method.
The API url being requested is correct. The request seems to be passing all the correct headers according to Twitter’s documentation.
I’ve tried to use the APIGee console to make the same API call and see what I’m doing wrong, but even that returns 404.
What am I doing wrong here?
DOH. It turns out the Twitter API URLs for OAuth operations are in fact slightly different to those for all other Twitter API calls and omit the API version segment, so this was a genuine 404.
There was also a tidier way to supply the
oauth_callbackparameter built into the RestSharp authenticator constructor—for reference, the code below works perfectly: