1. 程式人生 > >Yii2.0 restfut接口認證錯誤出坑指南

Yii2.0 restfut接口認證錯誤出坑指南

public restfu 參數 alt ica url and ESS for

測試Yii2.0即接口認證功能(出坑指南):
//認證

bindValTap: function () {
var page = this;
wx.request({
url: ‘http://apitest.test/invmembers?access-token=rg1v5BF7OBGE4Bze562wZSUfiStakUK_‘,
header: {
‘content-type‘: ‘application/json‘
},
method: ‘get‘,
data: {
‘per-page‘: 6,
// page: 5
},
success: function (res) {
// page.setData({motto:res.data[0].name});

console.log(res.data);
}
})
},

出現Yii2.0 restfut接口認證錯誤:Your request was made with invalid

研究了很久,後來找到\vendor\yiisoft\yii2\filters\auth\QueryParamAuth.php 中

class QueryParamAuth extends AuthMethod
{
/**

  • @var string the parameter name for passing the access token
    */
    public $tokenParam = ‘access-token‘;

    /**

  • @inheritdoc

    */
    public function authenticate($user, $request, $response)
    {
    $accessToken = $request->get($this->tokenParam);
    if (is_string($accessToken)) {
    $identity = $user->loginByAccessToken($accessToken, get_class($this));
    if ($identity !== null) {
    return $identity;
    }
    }
    if ($accessToken !== null) {
    $this->handleFailure($response);
    }

    return null;

    }
    }

才發現傳遞的參數是access-token,而不是access_token!

Yii2.0 restfut接口認證錯誤出坑指南