首页 文章

在laravel api调用中显示{“error”:“Unauthenticated . ”}

提问于
浏览
0

显示{“error”:“Unauthenticated . ”},同时使用ajax调用在Laravel 5.4和护照版本v1.0.9中调用larvel API .

从路由api调用:Route :: get('category / get_tree_data','CategoryApiController @ getTreeData') - > middleware('auth:api');

3 回答

  • 0

    如果你正确设置了Laravel Passport,你应该在你的视图中有这个:
    enter image description here

    您需要创建一个客户端,该客户端具有客户端ID和客户端密钥 .

    现在您需要打开您的消费者应用程序,其中包含您的客户端ID和您的客户端密码 .

    它看起来像这样(您必须将令牌和ID更改为您的特定码):

    class OAuthController extends Controller
    {
        public function redirect()
        {
            $query = http_build_query([
                'client_id' => 3,
                'redirect_uri' => 'http://localhost/app/public/callback',
                'response_type' => 'code',
                'scope' => '',
            ]);
    
            return redirect('http://localhost/app/public/oauth/authorize?' . $query);
        }
    
        public function callback(Request $request)
        {
            $http = new Client;
    
            $response = $http->post('http://localhost/app/public/oauth/token', [
                'form_params' => [
                    'grant_type' => 'authorization_code',
                    'client_id' => 3, // from admin panel above
                    'client_secret' => 'BcTgzw6YiwWUaU8ShX4bMTqej9ccgoA4NU8a2U9j', // from admin panel above
                    'redirect_uri' => 'http://localhost/app/public/callback',
                    'code' => $request->code // Get code from the callback
                ]
            ]);
    
            return json_decode((string) $response->getBody(), true);
        }
    }
    

    现在,您需要调用该消费者应用程序并授权您的应用程序 .

    enter image description here

    如果有效,您将获得一个访问令牌刷新令牌 .

    它应该如下所示:

    enter image description here

    现在你可以使用postman这样的程序来测试它 .

    你基本上调用你的get路由并添加访问令牌,这使你可以访问api,如下所示:

    enter image description here

    如果您还有其他问题,我建议您阅读docs .

    因此,我强烈建议观看Taylor Otwell的video .

    当然,如果您有任何疑问,也可以给我一个评论 .

  • 0

    超出护照集成并附加CSRF Headers 问题 .

    一种可能的解决方案是将中间件定义从 Route 声明移动到控制器构造函数 .

    这个:

    Route::middleware('auth:api', 'throttle:60,1')->group(function () {
        Route::get('tasks', 'Api\TasksController@index')->name('tasks');
    });
    

    删除 auth 中间件后:

    Route::middleware('api', 'throttle:60,1')->group(function () {
        Route::get('tasks', 'Api\TasksController@index')->name('tasks');
    });
    

    并在控制器构造函数上进行设置:

    <?php
    namespace App\Http\Controllers\Api;
    use App\Http\Controllers\Controller;
    class TasksController extends Controller
    {
        function __construct()
        {
            $this->middleware('auth');
        }
        public function index() { // ...
        }
    

    对不起,我无法解释这个调整的工作方式或原因 . 希望我稍后会更新这个以便更好地解释..

  • 0

    /resources/assets/js/bootstrap.js 文件中添加此代码

    window.axios.defaults.headers.common = {
        'X-CSRF-TOKEN': document.querySelector('meta[name="csrf-token"]').getAttribute('content'),
        'X-Requested-With': 'XMLHttpRequest'
    };
    

    完美地工作......

相关问题