我正试图通过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.
错误。使用--password
创建了密码客户端。
答案 0 :(得分:1)
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' => '',
],
]);
发送
application/x-www-form-urlencoded
POST请求要求您在form_params
请求选项中将POST字段指定为数组。$response = $client->request('POST', 'http://httpbin.org/post', [ 'form_params' => [ 'field_name' => 'abc', 'other_field' => '123', 'nested_field' => [ 'nested' => 'hello' ] ] ]);
下面显示了如何使用HttpClient
和FormUrlEncodedContent
发出密码授予令牌请求
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.