2015-07-13 16:26:47 24038次浏览 3条回答 2 悬赏 20 金钱

我需要做一个认证,浏览器输入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;
    }

}

  • 回答于 2015-07-13 17:11 举报

    虽然不懂Yii2,不过好像意思是你GET请求了应该POST或者PATCH的玩意儿。

    2 条回复
    回复于 2015-07-13 17:26 回复

    ..... 是get,不过不是这个问题,localhost/master2/api/web/v1/users?access_token=123
    access_token=123应该改为access-token=123,下划线错误,还有main.php的identityClass配置有问题
    解决一个问题又出现另一个问题....

    回复于 2017-11-23 18:44 回复

    我穿的参数是access-token,还是一直提示认证不通过,HttpBearerAuth和QueryParamAuth都试过了,传的access-token也是正确的值

  • 回答于 2015-12-15 15:27 举报

    请问你这个问题解决了吗

    5 条回复
    回复于 2015-12-15 15:31 回复

    解决了,是我写的参数有问题 access_token 应该改为 access-token

    回复于 2015-12-15 15:54 回复

    可是我的怎么 一直是401错 ?r=member/index&access_token=100-token

    {
    "name": "Unauthorized",
    "message": "You are requesting with an invalid credential.",
    "code": 0,
    "status": 401,
    "type": "yii\web\UnauthorizedHttpException"
    }

    回复于 2015-12-16 09:35 回复


    我使用的是

     'authMethods' => [
    		    	  QueryParamAuth::className(),
    ],
    
    

    它接收的参数是access-token 是横杠!!注意不是下划线!注意不是下划线!注意不是下划线!

    class QueryParamAuth extends AuthMethod
    {
        /**
         * @var string the parameter name for passing the access token
         */
        public $tokenParam = 'access-token';
    
    
    回复于 2015-12-21 14:04 回复

    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这个类你自定义的是怎么用?

    回复于 2019-12-08 00:18 回复

    我也出现这样的问题了

  • 回答于 2017-11-04 01:58 举报

    如果试了上诉方法依旧是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
主管

lgd5555

注册时间:2015-07-06
最后登录:2021-06-18
在线时长:18小时40分
  • 粉丝5
  • 金钱650
  • 威望0
  • 积分830

热门问题