Pri*_*egi 2 php api single-sign-on laravel guzzle
我已经在 Laravel https://sso/{custom_path}/token 中使用 Api 进行单点登录选项,就像使用 jwt 创建的 Api 一样。\n最后在 Web 应用程序中将标头中的访问令牌和内容类型传递给 Api 调用使用http客户端guzzle。\n使用内容类型application/x-www-form-urlencoded,并在form_params中使用参数。\n但作为响应,我缺少grant_type。当我在 form_parms 数组中传递 grant_type 时。有没有其他方法可以解决这个问题。任何有价值的回应都会被考虑。
\n代码:
\n$uri = $this->userTokenAuthencticateUrl();\n $token = session('token')->access_token;\n $params['header'] = [\n "Content-Type: application/x-www-form-urlencoded",\n "Authorization: Bearer $token"\n ];\n $params['form_params'] = array(\n 'grant_type' => 'xxxxx',\n 'response_include_resource_name' => 'xxx',\n 'audience' => 'xxxx', \n 'permission' => 'xxxxxx',\n );\n $response = Http::post($uri, $params);\n dd($response->json());\nRun Code Online (Sandbox Code Playgroud)\n回应:
\narray:2 [\xe2\x96\xbc\n "error" => "invalid_request"\n "error_description" => "Missing form parameter: grant_type"\n]\nRun Code Online (Sandbox Code Playgroud)\n
当您使用 HTTP 客户端时。您需要更改您的代码。您不需要application/x-www-form-urlencoded在标头中传递 Content-Type,并且我相信授权令牌是在标头中单独传递的,您可以将其传递到参数中。
$uri = $this->userTokenAuthencticateUrl();
$token = session('token')->access_token;
$params = array(
'grant_type' => 'xxxxx',
'response_include_resource_name' => 'xxx',
'audience' => 'xxxx',
'permission' => 'xxxxxx',
);
$response = Http::asForm()->withHeaders([
'Authorization' => 'Bearer ' . $token
])->post($uri, $params);
dd($response->json());
Run Code Online (Sandbox Code Playgroud)
如果您想快速将授权不记名令牌标头添加到请求中,您可以使用 withToken 方法,这样您也可以这样做
$uri = $this->userTokenAuthencticateUrl();
$token = session('token')->access_token;
$params = array(
'grant_type' => 'xxxxx',
'response_include_resource_name' => 'xxx',
'audience' => 'xxxx',
'permission' => 'xxxxxx',
);
$response = Http::asForm()->withToken($token)->post($uri, $params);
dd($response->json());
Run Code Online (Sandbox Code Playgroud)
请参阅文档了解更多详细信息
define("form_params", \GuzzleHttp\RequestOptions::FORM_PARAMS );
try{
$client = new \GuzzleHttp\Client(['headers' => ['Authorization' => 'Bearer ' . $token]]);
$guzzleResponse = $client->post(
$api_url, [
'form_params' => [
'grant_type' => 'xxxxx',
'response_include_resource_name' => 'xxx',
'audience' => 'xxxx',
'permission' => 'xxxxxx'
]
]);
if ($guzzleResponse->getStatusCode() == 200) {
$response = json_decode($guzzleResponse->getBody(),true);
//perform your action with $response
}
}
catch(\GuzzleHttp\Exception\RequestException $e){
// you can catch here 400 response errors and 500 response errors
// see this /sf/ask/1752830551/
}catch(Exception $e){
//other errors
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
7063 次 |
| 最近记录: |