我的代码有些困惑,我正在使用Guzzle Http Client版本6.3.3运行Laravel 6。
我选择使用在我的API网关上用于与微服务通信的特征,而不是使用重复的代码来膨胀代码库。
特质
public function performRequest($method, $requestUrl, $formParams = [], $headers =[])
{
$core = env('CORE_URI');
$client = new Client([
'base_uri' => $core,
]);
$response = $client->request($method, $requestUrl, ['form_params' => $formParams, 'headers' => $headers]);
return $response->getBody()->getContents();
}
失败的代码(即使使用邮递员也无法发送OAuth授权类型密码)
$core_client_id = env('CORE_CLIENT_ID');
$core_client_secret = env('CORE_CLIENT_SECRET');
$username = $request->input('username');
$password = $request->input('password');
return $this->performRequest('POST','/oauth/token', [
'form_params' => [
'grant_type' => 'password',
'client_id' => $core_client_id,
'client_secret' => $core_client_secret,
'username' => $username,
'password' => $password,
'scope' => '',
],
'headers' => [
'content-type' => 'multipart/form-data',
]
]);
异常口将返回400错误的请求'不支持的授予类型'
我在互联网上进行了一些搜索,发现标头Content-Type的OAuth2规范是“ application / x-www-form-urlencoded”。要解决您的问题,只需从'content-type' => 'multipart/form-data'
]中删除'headers'
这里是完整的代码
$core_client_id = env('CORE_CLIENT_ID');
$core_client_secret = env('CORE_CLIENT_SECRET');
$username = $request->input('username');
$password = $request->input('password');
return $this->performRequest('POST','/oauth/token', [
'form_params' => [
'grant_type' => 'password',
'client_id' => $core_client_id,
'client_secret' => $core_client_secret,
'username' => $username,
'password' => $password,
'scope' => '',
],
]);