title: override OAuth2 token endpoint request .net core
body: <p>I'm trying to use Ebay's OAuth2 flow in .net core 2 by registering the authentication middleware.    The authorization succeeded, but getting this error when the middleware is processing the token request (after receiving code from authorization service).</p>
  
  <pre><code>Exception: OAuth token endpoint failure: Status: BadRequest;Headers: 
  Connection: keep-alive
  Date: Tue, 27 Mar 2018 14:42:38 GMT
  ...  
  Body: {"error":"invalid_request","error_description":"request is missing a 
  required parameter or malformed."};
  </code></pre>
  
  <p>eBay's requirements for the access token request are </p>
  
  <pre class="lang-none prettyprint-override"><code>HTTP method: POST  
  URL (Sandbox): https://api.sandbox.ebay.com/identity/v1/oauth2/token
  HTTP headers:  
    Content-Type = application/x-www-form-urlencoded  
    Authorization = Basic 'B64-encoded-oauth-credentials'
  
  Request body (wrapped for readability):  
  grant_type=authorization_code&amp;  
  code=&lt;authorization-code-value&gt;&amp;  
  redirect_uri='RuName-value'  
  </code></pre>
  
  <p>seems like the request token is not in the right format, wherein the code can I  format the required body and headers for the token request. </p>
  
  <p>attaching my code <a href="https://i.stack.imgur.com/SXtch.png" rel="nofollow noreferrer">my code</a> </p>
  
  <p>code:</p>
  
  <pre><code>public void ConfigureServices(IServiceCollection services)
      {
          services.AddMvc();
  
          services.Configure&lt;MvcOptions&gt;(options =&gt;
          {
              options.Filters.Add(new RequireHttpsAttribute());
              options.SslPort = 44321;
          });
  
          services.AddAntiforgery(
              options =&gt;
              {
                  options.Cookie.Name = "_af";
                  options.Cookie.HttpOnly = true;
                  options.Cookie.SecurePolicy = CookieSecurePolicy.Always;
                  options.HeaderName = "X-XSRF-TOKEN";
              });
  
          // authentication middleware
          services.AddAuthentication(options =&gt;
          {
              options.DefaultAuthenticateScheme = CookieAuthenticationDefaults.AuthenticationScheme;
              options.DefaultSignInScheme = CookieAuthenticationDefaults.AuthenticationScheme;
              options.DefaultChallengeScheme = "Ebay";
          })
      .AddCookie()
      .AddOAuth("Ebay", options =&gt;
      {
          options.ClientId = "my-client-id";
          options.ClientSecret = "my-secret";
          options.CallbackPath = "/signin-Ebay";
  
          options.AuthorizationEndpoint = "https://signin.ebay.com/authorize?redirect_uri=my-uri";
          options.TokenEndpoint = "https://api.ebay.com/identity/v1/oauth2/token";
          options.Scope.Add("https://api.ebay.com/oauth/api_scope");
  
          options.Events = new OAuthEvents
          {
              OnCreatingTicket = async context =&gt;
              {
                  var request = new HttpRequestMessage(HttpMethod.Get, context.Options.UserInformationEndpoint);
                  request.Headers.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
                  request.Headers.Authorization = new AuthenticationHeaderValue("Bearer", context.AccessToken);
  
                  var response = await context.Backchannel.SendAsync(request, HttpCompletionOption.ResponseHeadersRead, 
                                                                      context.HttpContext.RequestAborted);
                  response.EnsureSuccessStatusCode();
  
                  var user = JObject.Parse(await response.Content.ReadAsStringAsync());
  
                  context.RunClaimActions(user);
              }
          };
      });
  
  
      }
  </code></pre>