title: How to get token with C# WebApi Client (Owin)
body: <p>I am using Owin.Oauth for authorization. When i send this parameters with postman i can get json data from webapi. the url is <a href="http://localhost:53415/token" rel="nofollow noreferrer">http://localhost:53415/token</a></p>
  
  <p><strong>Headers (Key-Value)</strong></p>
  
  <pre><code>Accept           application/json
  Content-Type     application/x-www-form-urlencoded
  </code></pre>
  
  <p><strong>Body</strong></p>
  
  <pre><code>grant_type       password
  username         user_1
  password         123456
  </code></pre>
  
  <p>The result is below</p>
  
  <pre><code>{
      "access_token": "Q0G_r6iLTFk1eeZedyL4JC0Z6Q-sBwVmtSasrNm8Yb1MCscDkeLiXugKrXq236LEJK6vM8taXf9cfWhCKRTcBWrQ14x5FOFKE1oV5xdW8VKZL8LZSzsvEwzP5Rr7G4lnkakxcsbu151LkkmM_dIF3Rx9_cvk0z1TKUznm9Ke_jxKgjichd-8fmdsupmysuP00biNuT6PYZPHiMYXaON2YiCK67A1yGHb-X2GhBL6NWc",
      "token_type": "bearer",
      "expires_in": 86399
  }
  </code></pre>
  
  <p>So i am trying to make this in C# MVC client. I created an api helper to make this and here is my tried code below.</p>
  
  <pre><code>                ApiHelper&lt;CampaignList&gt; _api = new ApiHelper&lt;CampaignList&gt;();
                  JObject oJsonObject = new JObject();
                  oJsonObject.Add("grant_type", "password");
                  oJsonObject.Add("username", "user_1");
                  oJsonObject.Add("password", "123456");
                  _api.GetToken(oJsonObject);
  </code></pre>
  
  <p>Now in ApiHelper the code is this :</p>
  
  <pre><code>HttpClient client = new HttpClient();
  client.BaseAddress = new Uri(WebConfigurationManager.AppSettings["apiUrl"]);
  client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
  client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/x-www-form-urlencoded"));
  HttpResponseMessage response = client.PostAsJsonAsync("token", oJsonObject.ToString()).Result;
  if (response.IsSuccessStatusCode)
     {
         ApiTokenEntity _entity = response.Content.ReadAsAsync&lt;ApiTokenEntity&gt;().Result;
         return _entity;
     }
     else
     {
         return null;
     }
  </code></pre>
  
  <p>if (response.IsSuccessStatusCode) line has returned false and i couldn't get my token to return. So i need to get token to send with header. Where do I make wrong i dk.</p>
  
  <pre><code>public class ApiTokenEntity
  {
      [JsonProperty(PropertyName = "access_token")]
      public string AccessToken { get; set; }
  
      [JsonProperty(PropertyName = "expires_in")]
      public int ExpiresIn { get; set; }
  
      [JsonProperty(PropertyName = "token_type")]
      public string TokenType { get; set; }
  
  }
  </code></pre>