在 Laravel api 调用中显示 {"error":"Unauthenticated."}

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/45152437/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 16:18:54  来源:igfitidea点击:

showing {"error":"Unauthenticated."} on laravel api call

laravellaravel-5.4laravel-passport

提问by leaveme_alone

showing {"error":"Unauthenticated."}, while calling larvel API in Laravel 5.4 and passport version: v1.0.9, using ajax call.

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

Calling from route api: Route::get('category/get_tree_data', 'CategoryApiController@getTreeData')->middleware('auth:api');

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

回答by utdev

If you set up Laravel Passport properly, you should have this in your view:enter image description here

如果您正确设置了 Laravel Passport,您应该看到以下内容:在此处输入图片说明

You need to create a client, that client has a client id and a client secret.

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

Now you need to open your consumer app which shall contain your client id and your client secret.

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

It looks like this(you have to change the token and id to your specific one):

它看起来像这样(您必须将令牌和 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);
    }
}

Now you need to call that consumer app and authorize your application.

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

enter image description here

在此处输入图片说明

If that worked you get an access token + a refresh token.

如果这样做有效,您将获得访问令牌 + 刷新令牌。

It should look like this:

它应该是这样的:

enter image description here

在此处输入图片说明

Now you can test this using a program like postman.

现在您可以使用 postman 之类的程序来测试它。

You basically call your get route and add the access token, which gives you access to the api, like this:

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

enter image description here

在此处输入图片说明

If you have any more question I recommend reading the docs.

如果您还有其他问题,我建议您阅读文档

Thus I highly recommend watching following videofrom Taylor Otwell.

因此,我强烈建议您观看Taylor Otwell 的以下视频

Of course you can give me a comment aswell if you have any more questions.

当然,如果你还有什么问题,也可以给我留言。

回答by leaveme_alone

Add this code inside your /resources/assets/js/bootstrap.jsfile

将此代码添加到您的/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'
};

working perfectly...

完美运行...

回答by arhakim

I have the same issue with Laravel 5.8 when calling /api/useron auth:apimiddleware, I've tried to call the API url using Postman but got unauthenticatedmessage

我有Laravel 5.8相同的问题时,呼吁/api/userauth:api中间件,我试着打电话给使用邮差API的网址,但得到unauthenticated的消息

And I am able to fixed it by changing the hashvalue to falsein config/auth.phpfile

我可以通过将hash值更改为falsein config/auth.phpfile来修复它

// routes/api.php
Route::middleware('auth:api')->get('/user', function (Request $request) {
    return $request->user();
});
// config/auth.php
'guards' => [
        'web' => [
            'driver' => 'session',
            'provider' => 'users',
        ],

        'api' => [
            'driver' => 'token',
            'provider' => 'users',
            'hash' => false, // <-- change this to false
        ],
    ],