当我尝试使用yaml文件中生成的php客户端时,它似乎不起作用。即使填写了身份验证的配置,我仍然收到身份验证错误。但是,当我将生成的API中的options变量替换为基本的auth选项时,它确实起作用。请参见下面的代码:
public function getLiveWegvakkenDataWithHttpInfo()
{
$returnType = '\Swagger\Client\Model\Wegvak[]';
$request = $this->getLiveWegvakkenDataRequest();
try {
$options = $this->createHttpClientOption();
try {
$response = $this->client->send($request, $options);
} catch (RequestException $e) {
throw new ApiException(
"[{$e->getCode()}] {$e->getMessage()}",
$e->getCode(),
$e->getResponse() ? $e->getResponse()->getHeaders() : null,
$e->getResponse() ? $e->getResponse()->getBody()->getContents() : null
);
}
上面的代码不起作用,但是当我用以下命令替换$options
时:
[
'auth' => [
'username',
'password',
]]
它确实起作用。有人可以解释我做错了吗?既然对我来说,标准的实现不起作用似乎很奇怪。
答案 0 :(得分:1)
最后我发现我的yaml文件未包含此文件:https://swagger.io/docs/specification/authentication/basic-authentication/
在上面的文档中,它说您需要以下代码:
components:
securitySchemes:
basicAuth:
type: http
scheme: basic
security:
- basicAuth: []
添加此代码后,它将在API代码内部实现,以实际使用您在配置内部填写的基本身份验证。否则,它只会发出请求,而不会在请求标头中添加auth。
因此,添加完并填写配置后,它对我有用。