Home >Backend Development >C++ >How to Add an Authorization Header to an HttpClient Request in WinRT?
Use HttpClient in WinRT to make requests to the REST API and authenticate with OAuth
Question:
Developing REST API clients using HttpClient in WinRT requires using an OAuth token to authenticate requests. However, the .NET Credential class used to set the authorization header is not available in WinRT.
Solution:
To set the authorization header in WinRT, follow these steps:
<code class="language-csharp">// 将 "Your Oauth token" 替换为您的实际令牌。 httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", "Your Oauth token");</code>
Example:
<code class="language-csharp">// 假设您有一个名为 'httpClient' 的 HttpClient。 httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIxMjM0NTY3ODkwIiwibmFtZSI6IkpvaG4gRG9lIiwiaWF0IjoxNTE2MjM5MDIyfQ.SflKxwRJSMeKKF2QT4fwpMeJf36POk6yJV_adQssw5c");</code>
Note:
Replace "Your Oauth token" with the actual token you obtained from the OAuth request.
The above is the detailed content of How to Add an Authorization Header to an HttpClient Request in WinRT?. For more information, please follow other related articles on the PHP Chinese website!