WinRT's HttpClient
lacks the .NET Credential
class for setting authorization headers. This article details a workaround for adding authorization headers, specifically using OAuth tokens.
The Challenge: How to add an authorization header (e.g., for OAuth) to a WinRT HttpClient
request?
The Solution: Utilize the AuthenticationHeaderValue
class:
httpClient.DefaultRequestHeaders.Authorization =
new AuthenticationHeaderValue("Bearer", "Your OAuth token");
This code snippet constructs an authentication header using the "Bearer" scheme and your OAuth token. Adding this to DefaultRequestHeaders.Authorization
ensures the header is automatically included in all subsequent requests made by this HttpClient
instance.
Disclaimer: All resources provided are partly from the Internet. If there is any infringement of your copyright or other rights and interests, please explain the detailed reasons and provide proof of copyright or rights and interests and then send it to the email: [email protected] We will handle it for you as soon as possible.
Copyright© 2022 湘ICP备2022001581号-3