title: Invalid request when requesting Access token from API: C#, Azure Function, OAuth
body: <p>I am getting &quot;Bad request: invalid response&quot; when I request an Access Token programmatically to the token endpoint. The parameters need to be passed as x-www-formurlencoded</p>
  <p>Here is my code:</p>
  <pre><code>[FunctionName(&quot;GetAccessToken&quot;)]
      public async Task&lt;IActionResult&gt; GetAccessToken(
    [HttpTrigger(AuthorizationLevel.Anonymous, &quot;post&quot;, Route = null)] HttpRequest req,
    ILogger log)
      {
          try
          {
              log.LogInformation(&quot;C# HTTP trigger function ''GetAccessToken'' processed a request.&quot;);
  
              string clientSecret = &quot;secret&quot;;
  
              string accessToken = &quot;&quot;;     
  
              RequestAccessToken rT = new RequestAccessToken();
              rT.Code = req.Form[&quot;code&quot;];
              rT.RedirectUri = req.Form[&quot;redirect_uri&quot;];
              rT.GrantType = req.Form[&quot;grant_type&quot;];
              rT.ClientId = req.Form[&quot;client_id&quot;];
              rT.CodeVerifier = req.Form[&quot;code_verifier&quot;];
              //rT.Scope = req.Form[&quot;scope&quot;];
  
              using (var client = new HttpClient())
              {
                  client.BaseAddress = new Uri(&quot;https://&lt;base_uri&gt;&quot;);
                  
  
                  //client.DefaultRequestHeaders
                  //.Accept
                  //.Add(new MediaTypeWithQualityHeaderValue(&quot;application/x-www-form-urlencoded&quot;));//ACCEPT header
                  
  
                  var body = new { EntityState = new {
                      code = rT.Code,
                      redirect_uri = rT.RedirectUri,
                      grant_type = rT.GrantType,
                      client_id = rT.ClientId,
                      client_secret = clientSecret,
                      code_verifier = rT.CodeVerifier,
                      
  
                  } }.ToString();
  
  
                  var data = new StringContent(body, Encoding.UTF8, &quot;application/x-www-form-urlencoded&quot;);
  
                  var result = await client.PostAsJsonAsync(
                                 &quot;/login&quot;,
                                 body);
                  accessToken = await result.Content.ReadAsStringAsync();
              }             
  
              return new OkObjectResult(accessToken);
  
          }
          catch (Exception ex)
          {
              log.LogInformation(ex.ToString());
  
              return new ObjectResult(ex.ToString()) { StatusCode = 500 };
          }
      }
  </code></pre>
  <p>Here is the call from the react app:
  <a href="https://i.stack.imgur.com/fye1S.jpg" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/fye1S.jpg" alt="enter image description here" /></a></p>
  <p>Can you tell me what where I am mistakening? Or provide me an example?</p>
  <p>New suggested version:</p>
  <pre><code>[FunctionName(&quot;GetAccessToken&quot;)]
      public async Task&lt;IActionResult&gt; GetAccessToken(
    [HttpTrigger(AuthorizationLevel.Anonymous, &quot;post&quot;, Route = null)] HttpRequest req,
    ILogger log)
      {
          try
          {
              log.LogInformation(&quot;C# HTTP trigger function ''GetAccessToken'' processed a request.&quot;);
  
              string clientSecret = &quot;secret&quot;;
  
              string accessToken = &quot;&quot;;
              HttpResponseMessage response = null;
  
              RequestAccessToken rT = new RequestAccessToken();
              rT.Code = req.Form[&quot;code&quot;];
              rT.RedirectUri = req.Form[&quot;redirect_uri&quot;];
              rT.GrantType = req.Form[&quot;grant_type&quot;];
              rT.ClientId = req.Form[&quot;client_id&quot;];
              rT.CodeVerifier = req.Form[&quot;code_verifier&quot;];
              //rT.Scope = req.Form[&quot;scope&quot;];
  
             
  
              using (var client = new HttpClient())
              {
                  client.BaseAddress = new Uri(&quot;https://&lt;base_uri&gt;&quot;);
                 
  
                  requestObject.Add(&quot;code&quot;, rT.Code);
                  requestObject.Add(&quot;redirect_uri&quot;, rT.RedirectUri);
                  requestObject.Add(&quot;grant_type&quot;, rT.GrantType);
                  requestObject.Add(&quot;client_id&quot;, rT.ClientId);
                  requestObject.Add(&quot;client_secret&quot;, clientSecret);
                  requestObject.Add(&quot;code_verifier&quot;, rT.CodeVerifier);
  
  
                  var boundary = Guid.NewGuid().ToString();
                  using (var multiForm = new MultipartFormDataContent(boundary))
                  {
                      foreach (var keyValuePair in (Dictionary&lt;string, string&gt;)requestObject)
                      {
                          multiForm.Add(new StringContent(keyValuePair.Value), String.Format(&quot;\&quot;{0}\&quot;&quot;, keyValuePair.Key));
                      }
  
                      response = client.PostAsync(&quot;/login&quot;, multiForm).Result;
                  }
  
  
              }             
  
              return new OkObjectResult(accessToken);
  
          }
          catch (Exception ex)
          {
              log.LogInformation(ex.ToString());
  
              return new ObjectResult(ex.ToString()) { StatusCode = 500 };
          }
      }
  </code></pre>