接口access_token认证401错误 [ 2.0 版本 ]
我需要做一个认证,浏览器输入localhost/master2/api/web/v1/users?access_token=123
请问这是什么问题
<response>
<name>Unauthorized</name>
<message>You are requesting with an invalid credential.</message>
<code>0</code>
<status>401</status>
<type>yii\web\UnauthorizedHttpException</type>
</response>
User model
use \yii\db\ActiveRecord;
use Yii;
use yii\web\IdentityInterface;
class User extends ActiveRecord implements IdentityInterface
{
public static function findIdentity($id)
{
return static::findOne($id);
}
public function getId()
{
return $this->id;
}
public function getAuthKey()
{
return $this->auth_key;
}
public function validateAuthKey($authKey)
{
return $this->getAuthKey() === $authKey;
}
public static function findIdentityByAccessToken($token, $type = null)
{
return static::findOne(['access_token' => $token]);
}
/**
* @inheritdoc
*/
public static function tableName()
{
return '{{%user}}';
}
/**
* @inheritdoc
*/
public static function primaryKey()
{
return ['uid'];
}
/**
* Define rules for validation
*/
public function rules()
{
return [
[['username', 'password', 'email', 'created_at', 'updated_at'], 'required'],
[['age', 'status', 'created_at', 'updated_at'], 'integer'],
];
}
}
UserController
use Yii;
use yii\rest\ActiveController;
use yii\filters\auth\QueryParamAuth;
use yii\filters\auth\CompositeAuth;
use yii\filters\auth\HttpBasicAuth;
class UserController extends ActiveController
{
public $modelClass = 'api\modules\v1\models\User';
public $serializer = [
'class' => 'yii\rest\Serializer',
'collectionEnvelope' => 'items',
];
public function behaviors()
{
$behaviors = parent::behaviors();
$behaviors['authenticator'] = [
'class' => CompositeAuth::className(),
'authMethods' => [
QueryParamAuth::className(),
],
];
return $behaviors;
}
}
共 3 个回答
-
请问你这个问题解决了吗
共 5 条回复An Error occurred while handling another error:
exception 'yii\base\InvalidRouteException' with message 'Unable to resolve the request "site/error".' in E:\work\wamp\www\lepa\vendor\yiisoft\yii2\base\Module.php:461
Stack trace:
#0 E:\work\wamp\www\lepa\vendor\yiisoft\yii2\web\ErrorHandler.php(85): yii\base\Module->runAction('site/error')
#1 E:\work\wamp\www\lepa\vendor\yiisoft\yii2\base\ErrorHandler.php(109): yii\web\ErrorHandler->renderException(Object(yii\web\UnauthorizedHttpException))
#2 [internal function]: yii\base\ErrorHandler->handleException(Object(yii\web\UnauthorizedHttpException))
#3 {main}
Previous exception:
exception 'yii\web\UnauthorizedHttpException' with message 'You are requesting with an invalid credential.' in E:\work\wamp\www\lepa\vendor\yiisoft\yii2\filters\auth\AuthMethod.php:73
Stack trace:
#0 E:\work\wamp\www\lepa\vendor\yiisoft\yii2\filters\auth\AuthMethod.php(56): yii\filters\auth\AuthMethod->handleFailure(Object(yii\web\Response))
#1 E:\work\wamp\www\lepa\vendor\yiisoft\yii2\filters\auth\CompositeAuth.php(57): yii\filters\auth\AuthMethod->beforeAction(Object(yii\base\InlineAction))
#2 E:\work\wamp\www\lepa\vendor\yiisoft\yii2\base\ActionFilter.php(71): yii\filters\auth\CompositeAuth->beforeAction(Object(yii\base\InlineAction))
#3 [internal function]: yii\base\ActionFilter->beforeFilter(Object(yii\base\ActionEvent))
#4 E:\work\wamp\www\lepa\vendor\yiisoft\yii2\base\Component.php(541): call_user_func(Array, Object(yii\base\ActionEvent))
#5 E:\work\wamp\www\lepa\vendor\yiisoft\yii2\base\Controller.php(263): yii\base\Component->trigger('beforeAction', Object(yii\base\ActionEvent))
#6 E:\work\wamp\www\lepa\vendor\yiisoft\yii2\web\Controller.php(108): yii\base\Controller->beforeAction(Object(yii\base\InlineAction))
#7 E:\work\wamp\www\lepa\vendor\yiisoft\yii2\base\Controller.php(149): yii\web\Controller->beforeAction(Object(yii\base\InlineAction))
#8 E:\work\wamp\www\lepa\vendor\yiisoft\yii2\base\Module.php(455): yii\base\Controller->runAction('loging', Array)
#9 E:\work\wamp\www\lepa\vendor\yiisoft\yii2\web\Application.php(84): yii\base\Module->runAction('member/loging', Array)
#10 E:\work\wamp\www\lepa\vendor\yiisoft\yii2\base\Application.php(375): yii\web\Application->handleRequest(Object(yii\web\Request))
#11 E:\work\wamp\www\lepa\api\web\index.php(18): yii\base\Application->run()
#12 {main}下划线也是错的。。。QueryParamAuth这个类你自定义的是怎么用?
-
如果试了上诉方法依旧是401 error, 我通过下面方法解决了,可能不一定适用,但可以试试
找一个2.0.10 或者2.0.9的yii2 版本, 拷贝 vendor/yiisoft 文件夹覆盖你的工作版本的vendor/yiisoft , 我试了3个服务器和localhost, 都是这个问题。
怀疑是2.0.11的改动,导致之前的某个地方出问题了。
别忘了保留 vendor/yiisoft/extensions.php, 否则很多安装的plugin会不能用
do not forget to keep the original vendor/yiisoft/extensions.php or your installed plugins will missing
lgd5555
最后登录:2021-06-18
在线时长:18小时40分
- 粉丝5
- 金钱650
- 威望0
- 积分830