C# PostAsync to Laravel Passport 表示授权服务器不支持授权

C# PostAsync to Laravel Passport says Authorization server does not support grant

我正在尝试通过 C# 从 Passport 请求令牌,如下所示:

StringContent content = new StringContent("form_params: [{'grant_type':'password', 'username': "+UserName.Text+",'password':"+Password.Password+ ",'client_id':4,'client_secret':'YAYLOOKATTHISNOTWORKING','scope':''}]", Encoding.UTF8, "application/x-www-form-urlencoded");
HttpResponseMessage response = await App.client.PostAsync("https://" + InterfaceAddress.Text+"/oauth/token", content);
response.EnsureSuccessStatusCode();    // Throw if not a success code.

这总是会导致 The authorization grant type is not supported by the authorization server. 错误。 passpport 客户端是使用 --password 创建的。

content 的媒体类型说 application/x-www-form-urlencoded 但实际内容字符串看起来更像 JSON。

重新检查您发送内容的方式,因为这可能是格式问题。

基于 API documentation for requesting password grant token,密码授予令牌请求如下所示

$http = new GuzzleHttp\Client;

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

Guzzel documentation

Sending application/x-www-form-urlencoded POST requests requires that you specify the POST fields as an array in the form_params request options.

$response = $client->request('POST', 'http://httpbin.org/post', [
    'form_params' => [
        'field_name' => 'abc',
        'other_field' => '123',
        'nested_field' => [
            'nested' => 'hello'
        ]
    ]
]);

下面显示了如何使用 HttpClientFormUrlEncodedContent

发出密码授予令牌请求
var url = "https://" + InterfaceAddress.Text + "/oauth/token";
string username = UserName.Text;
string password =  Password.Password;

var form_params = new Dictionary<string,string>(){
    {"grant_type", "password"},
    {"username", username},
    {"password", password},
    {"client_id","4"},
    {"client_secret", "YAYLOOKATTHISNOTWORKING"},
    {"scope", ""}
};
var content = new FormUrlEncodedContent(form_params);    
var response = await App.client.PostAsync(url, content);
response.EnsureSuccessStatusCode();    // Throw if not a success code.