我想从Oauth服务器获取令牌。我使用用户名和密码登录浏览器,Oauth服务器返回此URL:
http:/example.com/callback?code=D3F7A9B42EA49F92EACC21ECC60AA7187A71DAD85E4478FB8724BB0444054D39&scope=tts_api&iss=https://oauthserver.com
用于处理此方法中使用的回调
public function handleAauth(Request $request)
{
$http = new Client;
$code_verifier = bin2hex(random_bytes(32));
$code_challenge = hash('sha256', $code_verifier, true);
$response = $http->post('https://outh-server.com/connect/token', [
'form_params' => [
'grant_type' => 'authorization_code',
'client_id' => 'my-client-id',
'code_verifier' => $code_verifier,
'code' => $request->code,
'code_challenge' => $code_challenge,
'redirect_uri' => 'http://example.com/callback',
],
'headers' => ['Accept' => 'application/json']
]);
return $response->getBody()->getContents();
}
当使用此代码进行回调时,返回此错误:
Client error: `POST https://outh-server.com/connect/token` resulted in a `400 Bad Request` response: {"error":"invalid_request"}
请帮帮我。
1条答案
按热度按时间rks48beu1#
另一种解决方案是使用
json
键而不是form_params
来将请求有效负载作为JSON对象发送。尝试向代码中添加更具体的错误处理,以获取有关错误响应的更多信息。例如,可以捕获
GuzzleHttp\Exception\ClientException
异常并检查响应正文和标头: