C# Twitter request OAuth Token -


i trying request token twitter api based on consumer key , consumer secret key. getting the remote server returned error: (403) forbidden not sure why?

this attempt far

//get request token string oauth_consumer_key = "<consumer key>"; string oauth_consumer_secret = "<consumer secret>";  uri requesttoken = new uri("https://api.twitter.com/oauth2/token?oauth_consumer_key=" + oauth_consumer_key + "&oauth_consumer_secret=" + oauth_consumer_secret); httpwebrequest req = (httpwebrequest)webrequest.create(requesttoken); req.method = "post";  try {   using (var response = req.getresponse() httpwebresponse)   if (req.haveresponse && response != null)   {    } } catch (webexception wex) { } 

the code incomplete running through seem forbidden exception?

if post url request follows, works fine , returns token

https://twitter.com/oauth/request_token?oauth_consumer_key=bidjtabokf0b3mvw1uahwdf7x&oauth_consumer_secret=qwo208qapzvckboywu3qet8ufnbxxlg3tstwss8oaotoy8qwhd

am doing wrong?

solved problem using task / asyc , adding authorization oauth headers. able access token

here solution:

public async task<actionresult> accesstoken() {                              var httpclient = new httpclient();       var request = new httprequestmessage(httpmethod.post, "https://api.twitter.com/oauth2/token");        string oauth_consumer_key = "<consumer key>";       string oauth_consumer_secret = "<consumer secret>";        string url = "https://api.twitter.com/oauth2/token?oauth_consumer_key=" + oauth_consumer_key + "&oauth_consumer_secret=" + oauth_consumer_secret;        var customerinfo = convert.tobase64string(new utf8encoding()                           .getbytes(oauth_consumer_key + ":" + oauth_consumer_secret));       // add authorization headers       request.headers.add("authorization", "basic " + customerinfo);       request.content = new stringcontent("grant_type=client_credentials", encoding.utf8,                                                           "application/x-www-form-urlencoded");        httpresponsemessage response = await httpclient.sendasync(request);        string json = await response.content.readasstringasync();       var serializer = new javascriptserializer();       dynamic item = serializer.deserialize<object>(json);        viewbag.access_token = item["access_token"];        return view();                 } 

Comments