Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to force WebRequest to send Authorization header during POST

When using WebRequest to send a POST, the Authorization header is not sent with the request even though I have manually set the header and set PreAuthenticate to true, eg:

webRequest.Headers["Authorization"] = "OAuth oauth_consumer_key=bFPD...";
webRequest.PreAuthenticate = true;

Using Fiddler I can see that the Authorization header is not sent. The target site (Twitter) returns a 400 (Bad request) rather than a 401 (Not authorized) which is therefore the incorrect challenge required for WebRequest to send the Authorization data. For information, the returned content is:

{"errors":[{"message":"Bad Authentication data","code":215}]}

So, how do I get around this? How can I force WebRequest to send the Authorization with initial request? Note that the authorization data is not Basic Authentication, rather it is an OAuth string.

Thanks

like image 821
chris Avatar asked Jan 16 '14 09:01

chris


People also ask

How do I send a post Authorization header?

To send a request with the Bearer Token authorization header, you need to make an HTTP request and provide your Bearer Token with the "Authorization: Bearer {token}" header. A Bearer Token is a cryptic string typically generated by the server in response to a login request.

Is Authorization header automatically sent?

The HTTP Authorization request header can be used to provide credentials that authenticate a user agent with a server, allowing access to a protected resource. The Authorization header is usually, but not always, sent after the user agent first attempts to request a protected resource without credentials.

How do I pass auth token in httpWebRequest?

var token = GetToken(username, password); // var httpWebRequest = (HttpWebRequest)WebRequest. Create( // "http://localhost/api/services/myApp/commonLookup/TestCallingRemotely"); // httpWebRequest. ContentType = "application/json"; // httpWebRequest. Method = "POST"; httpWebRequest.

How do I send Authorization header in post request Axios?

Here's how you can set the Authorization header, which is typically used to send access tokens to a server. // Send a GET request with the authorization header set to // the string 'my secret token' const res = await axios. get('https://httpbin.org/get', { headers: { authorization: 'my secret token' } });


3 Answers

Here's my solution. The value is in variable json.

var myUri = new Uri(fullpath);
var myWebRequest = WebRequest.Create(myUri);
var myHttpWebRequest = (HttpWebRequest)myWebRequest;
myHttpWebRequest.PreAuthenticate = true;
myHttpWebRequest.Headers.Add("Authorization", "Bearer " + AccessToken);
myHttpWebRequest.Accept = "application/json";

var myWebResponse = myWebRequest.GetResponse();
var responseStream = myWebResponse.GetResponseStream();
if (responseStream == null) return null;

var myStreamReader = new StreamReader(responseStream, Encoding.Default);
var json = myStreamReader.ReadToEnd();

responseStream.Close();
myWebResponse.Close();
like image 80
JuanPablo Avatar answered Oct 17 '22 16:10

JuanPablo


This drove me bonkers, but eventually found the answer in Adding Headers and Post data in RESTfull/HTTP Request in C#.

The solution for me was adding the Authorization Header BEFORE writing the request stream.

Hope this helps.

like image 9
stevefinn Avatar answered Oct 17 '22 16:10

stevefinn


Try this, but there should be no space between the authorization headers.

var authHeader = "OAuth  oauth_consumer_key=bFPD...";
webRequest.Headers.Add("Authorization", authHeader);
like image 3
You Avatar answered Oct 17 '22 16:10

You