php – 如何使用密码授予令牌的Laravel Passport?
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了php – 如何使用密码授予令牌的Laravel Passport?,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含3562字,纯文字阅读大概需要6分钟。
内容图文
![php – 如何使用密码授予令牌的Laravel Passport?](/upload/InfoBanner/zyjiaocheng/718/c6d8240e002d4e388c8d010d4a298e4c.jpg)
我刚刚阅读了https://laravel.com/docs/5.6/passport文档,我有些疑惑希望有人可以帮助我:
首先,在某些上下文中,我想使用Passport作为为我的移动应用程序(第一方应用程序)提供Oauth身份验证的方法.
>当我使用php artisan passport:client –password时,我会收到客户端ID和客户端密钥.这个值是否必须在我的应用上修复?例如,将它们存储为硬编码或“设置”文件?如果不应存储这些值,那么它应该如何工作?
>要将用户注册到我的应用程序,我使用:$user-> createToken(‘The-App’) – > accessToken;我得到的是accessToken将用于发送我的所有请求作为标题(Authorization => Bearer $accessToken),但究竟什么是“The-App”值?
>登录用户我正在使用URL:http://example.com/oauth/token并作为参数发送:
{
“username”:“user@email.com”,
“password”:“userpassword”,
“grant_type”:“密码”,
“client_id”:1,//我从命令中获得的客户端ID(问题1)
“client_secret”:“嘘”//我从命令中得到的客户端秘密(问题1)
}
>当我使用前一个端点登录用户时,我回到了refresh_token,我读到我可以通过http://example.com/oauth/token/refresh刷新令牌,但我尝试请求刷新我得到了错误419,我从csrf中删除了url oauth / token / refresh验证,现在我收到“消息”:“未经验证.”,我正在提出以下请求:
内容类型:x-www-form-urlencoded
grant_type:refresh_token
refresh_token:-refresh-token //我从命令中获取的刷新令牌(问题3)
client_id:1 //我从命令获得的客户端ID(问题1)
client_secret:嘘//我从命令得到的客户端秘密(问题1)
范围: ”
我应该使用这个端点吗?鉴于我正在努力开发的应用程序,或者没有必要.
>最后,我从护照中获得了许多端点,我认为不会使用这些端点,例如:oauth / clients *,oauth / personal-access-tokens *有没有办法将它们从已发布的端点中删除护照?
非常感谢你的帮助!
解决方法:
如果您正在使用自己的api,那么您不需要拨打http://example.com/oauth/token
用户登录,因为那时你需要在app端存储client_id和client_secret.最好为登录创建一个api,然后您可以检查凭据并生成个人令牌.
public function login(Request $request)
{
$credentials = $request->only('email', 'password');
if (Auth::attempt($credentials)) {
// Authentication passed...
$user = Auth::user();
$token = $user->createToken('Token Name')->accessToken;
return response()->json($token);
}
}
Finally, there are a lot of endpoints that I get from passport that I
don’t think I will use for example: oauth/clients*,
oauth/personal-access-tokens* is there a way to remove them from the
endpoints published by passport?
你需要删除Passport :: routes();来自AuthServiceProvider并手动输入所需的护照路线.我认为你只需要oauth / token路由.
what exactly is “The-App” value for?
如果你检查oauth_access_tokens表它有名字字段. $user-> createToken(‘令牌名称’) – > accessToken;这里存储在名称字段中的“令牌名称”.
How to use Laravel Passport with Password Grant Tokens?
要生成密码授予令牌,您必须在应用程序端存储client_id和client_secret(不推荐,请检查this)并假设您必须重置client_secret然后旧版本应用程序停止工作,这些都是问题.要生成密码授予令牌,您必须像在步骤3中提到的那样调用此API.
$http = new GuzzleHttp\Client;
$response = $http->post('http://your-app.com/oauth/token', [
'form_params' => [
'grant_type' => 'password',
'client_id' => 'client-id',
'client_secret' => 'client-secret',
'username' => 'taylor@laravel.com',
'password' => 'my-password',
'scope' => '',
],
]);
return json_decode((string) $response->getBody(), true);
Generate token from
refresh_token
$http = new GuzzleHttp\Client;
$response = $http->post('http://your-app.com/oauth/token', [
'form_params' => [
'grant_type' => 'refresh_token',
'refresh_token' => 'the-refresh-token',
'client_id' => 'client-id',
'client_secret' => 'client-secret',
'scope' => '',
],
]);
return json_decode((string) $response->getBody(), true);
你也可以看看这个https://laravel.com/docs/5.6/passport#implicit-grant-tokens.
内容总结
以上是互联网集市为您收集整理的php – 如何使用密码授予令牌的Laravel Passport?全部内容,希望文章能够帮你解决php – 如何使用密码授予令牌的Laravel Passport?所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。