title: C# Works with Nest API Restful Auth using HttpClient
body: <p>Still being new to C#, I am sure that I am not using the HttpClient library properly.  I am trying to authenticate with the Works With Nest API so that I can read/write requests to a thermostat.  Below is the code that I am using to authenticate:</p>
  
  <pre><code>using System;
  using System.Collections.Generic;
  using System.Linq;
  using System.Threading.Tasks;
  using Microsoft.AspNetCore.Mvc;
  using System.Net.Http;
  using System.Net.Http.Headers;
  
  
  namespace iConnect.Controllers
  {
      public class NestController : Controller
      {
          static HttpClient client = new HttpClient();
  
          public IActionResult Index()
          {
              return View();
          }
  
          public async Task&lt;HttpResponseMessage&gt; GetNestAuthCode()
          {
              // Nest Pin Code
              String pincode = "MYPING";
              String clientID = "My-Client-ID";
              String clientSecret = "MySecretString";
              String grantType = "authorization_code";
  
              client.BaseAddress = new Uri("https://api.home.nest.com");
              client.DefaultRequestHeaders.Accept.Clear();
              client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
              var request = new HttpRequestMessage(HttpMethod.Post, "/oauth2/access_token");
  
              var data = new List&lt;KeyValuePair&lt;string, string&gt;&gt;
              {                
                  new KeyValuePair&lt;string, string&gt;("code", pincode)
                  , new KeyValuePair&lt;string, string&gt;("client_id", clientID)
                  , new KeyValuePair&lt;string, string&gt;("client_secret", clientSecret)
                  , new KeyValuePair&lt;string, string&gt;("grant_type", grantType)
              };
  
              //var content = new FormUrlEncodedContent(data);
              //await content.ReadAsByteArrayAsync();
              //content.Add(data);
              request.Content = new FormUrlEncodedContent(data);
  
              //HttpResponseMessage response = await client.PostAsync(client.BaseAddress, content);
  
              HttpResponseMessage response = await client.SendAsync(request);
  
              return response;
          }
      }
  }
  </code></pre>
  
  <p>When I go to localhost:9387/Nest/GetAuthCode, I get the following JSON response:</p>
  
  <pre><code>{
     "version":{
        "major":1,
        "minor":1,
        "build":-1,
        "revision":-1,
        "majorRevision":-1,
        "minorRevision":-1
     },
     "content":{
        "headers":[
           {
              "key":"Content-Type",
              "value":[
                 "application/json"
              ]
           }
        ]
     },
     "statusCode":400,
     "reasonPhrase":"Bad Request",
     "headers":[
        {
           "key":"Connection",
           "value":[
              "keep-alive"
           ]
        }
     ],
     "requestMessage":{
        "version":{
           "major":1,
           "minor":1,
           "build":-1,
           "revision":-1,
           "majorRevision":-1,
           "minorRevision":-1
        },
        "content":{
           "headers":[
              {
                 "key":"Content-Type",
                 "value":[
                    "application/x-www-form-urlencoded"
                 ]
              },
              {
                 "key":"Content-Length",
                 "value":[
                    "130"
                 ]
              }
           ]
        },
        "method":{
           "method":"POST"
        },
        "requestUri":"https://api.home.nest.com/oauth2/access_token",
        "headers":[
           {
              "key":"Accept",
              "value":[
                 "application/json"
              ]
           }
        ],
        "properties":{
  
        }
     },
     "isSuccessStatusCode":false
  }
  </code></pre>
  
  <p>Any assistance is greatly appreciated.  Thank you.</p>
  
  <p><strong>EDIT:</strong></p>
  
  <p>I've made the following changes and get the following response (which is not what I expect):</p>
  
  <p>Code:</p>
  
  <pre><code>public async Task&lt;ActionResult&gt; GetNestAuthCode()
          {
              // Nest Pin Code
              String pincode = "MYPING";
              String clientID = "My-Client-ID";
              String clientSecret = "MySecretString";
              String grantType = "authorization_code";
  
              client.BaseAddress = new Uri("https://api.home.nest.com");
              client.DefaultRequestHeaders.Accept.Clear();
              client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
              //var request = new HttpRequestMessage(HttpMethod.Post, "/oauth2/access_token");
  
              var data = new List&lt;KeyValuePair&lt;string, string&gt;&gt;
              {                
                  new KeyValuePair&lt;string, string&gt;("code", pincode)
                  , new KeyValuePair&lt;string, string&gt;("client_id", clientID)
                  , new KeyValuePair&lt;string, string&gt;("client_secret", clientSecret)
                  , new KeyValuePair&lt;string, string&gt;("grant_type", grantType)
              };
  
              //var content = new FormUrlEncodedContent(data);
              //await content.ReadAsByteArrayAsync();
              //content.Add(data);
              //request.Content = new FormUrlEncodedContent(data);
  
              //HttpResponseMessage response = await client.PostAsync(client.BaseAddress, content);
  
              var response = await client.PostAsync("oauth2/access_token",
                      new FormUrlEncodedContent(data));
  
              var content = await response.Content.ReadAsStringAsync();
  
              return Content(content);
          }
  </code></pre>
  
  <p>Response:</p>
  
  <pre><code>{"error":"oauth2_error","error_description":"authorization code not found","instance_id":"f64d5268-8bec-4799-927c-e53454ed96d5"}
  </code></pre>