当我使用 QueryParamAuth 函数连接到我的 API 时,出现此错误:
“data”: { “name”: “Unauthorized”, “message”: “Your request was made with invalid credentials.”, “code”: 0, “status”: 401, “type”: “yii\web\UnauthorizedHttpException” }
这就是我的行为:
$behaviors['authenticator'] = [ 'class' => QueryParamAuth::class, 'tokenParam' => 'access-token', ];
所以我尝试使用令牌连接到我的 api。
需要去Yii2项目中的QueryParamAuth.php直接请求
acces-token
,无需经过tokenParam
声明。
public function authenticate($user, $request, $response)
{
$accessToken = $request->get('acces-token');
if (is_string($accessToken)) {
$identity = $user->loginByAccessToken($accessToken, get_class($this));
if ($identity !== null) {
return $identity;
}
}
if ($accessToken !== null) {
$this->handleFailure($response);
}
return null;
}