Answer the question
In order to leave comments, you need to log in
What is wrong with Yii2 rest api authorization?
I decided to play around with REST, figured it out to a greater extent, but got stuck with authorization. It seems to understand the principle, but apparently not correctly.
Here is the controller:
<?php
namespace frontend\controllers;
use yii;
use yii\rest\Controller;
use yii\filters\auth\QueryParamAuth;
class QuestionController extends Controller
{
public function behaviors()
{
$behaviors = parent::behaviors();
$behaviors['authenticator']['class'] = QueryParamAuth::className();
$behaviors['authenticator']['only'] = ['init'];
return $behaviors;
}
public function actionInit(){
return 124;
}
}
public static function findIdentityByAccessToken($token, $type = null)
{
return static::findOne(['auth_key' => $token]);
}
$(function() {
$.ajax('https://mysite/api/init',{
dataType: "json",
method: 'post',
data: {access_token:'1NLS4Os8zNwdeImN2hUUcaDpnTzNrfeM'},
success: function (data) {
$("#question").attr('data-key',data);
$("#loader").hide();
$("#test-body").show();
}
});
});
code:0
message:"Your request was made with invalid credentials."
name:"Unauthorized"
status:401
Answer the question
In order to leave comments, you need to log in
data: {access_token:'1NLS4Os8zNwdeImN2hUUcaDpnTzNrfeM'}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question