Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Setting Authorization Header of HttpClient

I have an HttpClient that I am using for a REST API. However I am having trouble setting up the Authorization header. I need to set the header to the token I received from doing my OAuth request. I saw some code for .NET that suggests the following,

httpClient.DefaultRequestHeaders.Authorization = new Credential(OAuth.token); 

However the Credential class does that not exist in WinRT. Anyone have any ideas how to set the Authorization header?

like image 956
Stephen Hynes Avatar asked Jan 31 '13 13:01

Stephen Hynes


People also ask

How do I add Auth header to HttpClient?

using (var client = new HttpClient()) { var url = "https://www.theidentityhub.com/{tenant}/api/identity/v1"; client. DefaultRequestHeaders. Add("Authorization", "Bearer " + accessToken); var response = await client.

How do I pass Authorization bearer in 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.

How do I change the Authorization header in URL?

It is indeed not possible to pass the username and password via query parameters in standard HTTP auth. Instead, you use a special URL format, like this: http://username:[email protected]/ -- this sends the credentials in the standard HTTP "Authorization" header.


2 Answers

So the way to do it is the following,

httpClient.DefaultRequestHeaders.Authorization =     new AuthenticationHeaderValue("Bearer", "Your Oauth token"); 
like image 154
Stephen Hynes Avatar answered Sep 23 '22 12:09

Stephen Hynes


request.DefaultRequestHeaders.Authorization =      new AuthenticationHeaderValue(         "Basic", Convert.ToBase64String(             System.Text.ASCIIEncoding.ASCII.GetBytes(                $"{yourusername}:{yourpwd}"))); 
like image 36
TheWhiteRabbit Avatar answered Sep 24 '22 12:09

TheWhiteRabbit