我对这个问题一直非常疯狂!它只是没有可怕的意义,为什么它在尝试击中这样的路线时不断吐出愚蠢的错误。经过多次搜索(多次搜索),我终于找到了答案。如果你从Laracasts看到这个费罗,你就是我的英雄!Unauthenticated
/api/user
你检查了吗?app\Providers\RouteServiceProvider.php
在 您可以设置中间件。检查以确保其 .如果不是,请更改它。此外,从路由文件中删除中间件。mapApiRoutes()
auth:api
auth
api.php
https://laracasts.com/discuss/channels/laravel/laravel-53-passport-api-unauthenticated-in-postman-using-personal-access-tokens
编辑:
对上述内容进行更改后,请继续执行以下示例。app\Providers\RouteServiceProvider.php
首先,我们需要检索一个新的.为此,我正在使用密码(更多信息:https://laravel.com/docs/5.4/passport#password-grant-tokensaccess_token
grant_type
)
为了检索一个新的,我创建了一个新的路由并调用它。然后,我将上述链接中的代码放入其中:access_token
routes/web.php
/connect
Route::get('connect', function (Request $request) {
$http = new GuzzleHttp\Client;
$response = $http->post('http://example.com/oauth/token', [
'form_params' => [
'grant_type' => 'password',
'client_id' => $request->client_id,
'client_secret' => $request->client_secret,
'username' => $request->username,
'password' => $request->password,
'scope' => ''
],
]);
return json_decode((string) $response->getBody(), true);
});
使用Chrome Postman,您需要:
- 将方法设置为
GET
- 输入具有相关参数的连接 URL,即 http://example.com/connect?client_id=1&client_secret=W2zogh7tiBh2jfHleYuzpViv7dqynDYQ6O07DKLj&username=test@test.com&password=123456
- 单击标题选项卡(它位于“授权”旁边),添加的键和值
Accept
application/json
- 点击发送按钮
示例结果:
{
"token_type": "Bearer",
"expires_in": 31535999,
"access_token": "eyJ0eXAiOiJKV1QiLCJhbGciOiJSUzI1NiIsImp0aSI6ImE0MmFiYjZkNTQ5M2ZjMGQxYzVmM2E3MDFlOTNjMTRlOTQxMTBmNWQ5NmI1ODI0NTBmMmEyM2MwMzQ5OTMwODdiZGUwYTI5ZDU5N2VjYTExIn0.eyJhdWQiOiIxIiwianRpIjoiYTQyYWJiNmQ1NDkzZmMwZDFjNWYzYTcwMWU5M2MxNGU5NDExM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WjWQMozddz5EWd4pvUI7J64Lw6cMCg/BslZLHtfN7IWoC1RQGp5K0cGO0QmZfsGMSzsoSUNFjv16BXiKSqlNvs5aGhxErFY4wEOKqBifXUkb3SwnK/iHKg3irmqj4fOf/aKNyCdd/PJCHrRPocrW83oM1sjq7eDufEIlgxmy7uRset8GLAWjx/n6rzkxz2QM0/9Lyc/XN9OL00XBYjA47a1wL55qUUUYWevaFwxWX8LG2UjBf9Vv2lfvLcBBkbgqpalePMDh6wb8IDyJek4BbvZtJ1VZ/l+A9XXY9rQt/hIDdoOAtib8CGr9/CERFIrByZa3TEJBCLAa2FvJSIhHVnKvnuvZX3e9qhTkgHqowJrWg2C3VyPDQYAdIhdpTEvs0pcGSAZWhwXfu9xKQOeyRTEScbLKQmuW+sGbwU+qfdLgh/BR5kW4TMer4TIzWKSuHsqmibgiUwaQkwTrtjH2Xz9Z9XmAbVzJ8pqbEZPe7t5whXDoRSnAwWymdxk2E7SiSsVUA3kX39="
}
突出显示该字符串并将其复制到文本编辑器。access_token
然后,您需要在 中创建一个新路由。下面的路由将简单地输出当前 API 用户的信息:routes/api.php
Route::group(['prefix' => 'user'], function() {
Route::get('/', function() {
return response()->json(request()->user());
});
});
完成上述操作后,对Postman进行以下更改:
- 将方法设置为
GET
- 将URL更改为指向API路由,即 http://example.com/api/user
- 再次单击标题选项卡,然后添加新的 key 和 value(替换为之前复制的访问令牌)
Authorization
Bearer access_token_here
access_token_here
- 点击发送按钮
输出示例:
{
"id": "75ced500-1544-11e7-9a8e-ad5f1aee389e",
"name": "test test",
"email": "test@test.com",
"created_at": "2017-03-30 23:29:03",
"updated_at": "2017-03-30 23:29:03"
}