0

My friend write API and sends such request to php

$response = $http->post('http://you-app.com/oauth/token',[
'form_params' => [
    'grant_type' => 'password',
    'client_id' => 'client-id',
    'client_secret' => 'client-secret',
    'username' => 'my-username',
    'password' => 'my-password',
    'scope' => '',
    ],
])

it`s my code (I work at xamarin.ios) maybe i'm sending the wrong request. (just starting to learn http request)

using (HttpClient client = new HttpClient { Timeout = TimeSpan.FromSeconds(60) })
{
//create my json
Request json_Request = new Request
    {
        Url = "http://my_url/oauth/token",
        Form_params = new Params
        {
            Grant_Type = "password",
            Client_ID = "my_client_id",
            Client_Secret = "my client secret",
            Username = "my username",
            Password = "my password",
            Scope = ""
        }
    };
    HttpContent content = new StringContent(JsonConvert.SerializeObject(json_Request), Encoding.UTF8);                
    content.Headers.ContentType = new System.Net.Http.Headers.MediaTypeHeaderValue("application/json");
    var response = await client.PostAsync(new Uri("http://my_url/oauth/token"), content);
    if (response.IsSuccessStatusCode)
    {
        var response_From_Server = await response.Content.ReadAsStreamAsync();
        return null;
    }
    else
    {
        return null;                    
    }
}
SyruS
  • 1
  • 1

2 Answers2

0

i'm use this and server send me 401 error)

var values = new Dictionary<string, string>
                {
                    {"grant_type", "password"},
                    {"client_id", "client-id"},
                    {"client_secret", "client-secret"},
                    {"username", "my username"},
                    {"password", "my password"},
                    {"scope", ""},
                };
request.Content = new FormUrlEncodedContent(values);
request.Content.Headers.ContentType = new System.Net.Http.Headers.MediaTypeHeaderValue("application/x-www-form-urlencoded");
HttpResponseMessage response = await client.PostAsync("http://inv/oauth/token", request.Content);
SyruS
  • 1
  • 1
0

The way you are creating the request is wrong. Pls see this post

https://stackoverflow.com/a/4015346/1184584

var values = new Dictionary<string, string>
{
   { "thing1", "hello" },
   { "thing2", "world" }
};

var content = new FormUrlEncodedContent(values);

var response = await client.PostAsync("http://www.example.com/recepticle.aspx", content);

var responseString = await response.Content.ReadAsStringAsync();
Nandun
  • 1,802
  • 2
  • 20
  • 35