OAuth2 – Pass Access Token – C#

Now that you have your Access Token – in case you missed it, here is the link to how to get it OAuth2: Get an Access Token – C#, you can now pass your authorization token to interact with your API.

Make sure to add a header to your request with your authorization token

     httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", authorizationToken);

The whole thing looks like this:

      string url = "your API URL";
      using (var httpClient = new HttpClient())
       {
           httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", authorizationToken);
           HttpResponseMessage response = await httpClient.GetAsync(url);
           var contents = await response.Content.ReadAsStringAsync();
           var model = JsonConvert.DeserializeObject<myObject>.contents);
           return myObject;
        }

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.