Php Laravel Passport密码授予-客户端身份验证失败
在听了很多关于laravel passport的事情后,我想到了在我的新项目中实现它,我的要求是创建一个将在移动应用程序中使用的API 因此,我的移动应用程序是一个Php Laravel Passport密码授予-客户端身份验证失败,php,laravel,oauth,laravel-5.3,Php,Laravel,Oauth,Laravel 5.3,在听了很多关于laravel passport的事情后,我想到了在我的新项目中实现它,我的要求是创建一个将在移动应用程序中使用的API 因此,我的移动应用程序是一个客户端,它将拥有更多的用户 我遵循这些步骤,也阅读了相关内容。简而言之,我遵循以下步骤: 已安装laravel/passport 创建了一个网站用户 生成的passport密钥php artisan passport:install 使用php artisan passport:client 在web.php 授权用户并获得最终访问令
客户端
,它将拥有更多的用户
我遵循这些步骤,也阅读了相关内容。简而言之,我遵循以下步骤:
laravel/passport
php artisan passport:install
php artisan passport:client
web.php
api/user
(使用头Authorization
包含值Bearer-eyJ0eXAiOiJKV1…(令牌)
我收到了数据。非常简单和整洁
但我的应用程序用户不会知道这些细节,所以我想配置一个完全符合我要求的应用程序
现在开始真正的头痛了。我在过去三天里一直在尝试设置这个,并且不断地得到解决
{"error":"invalid_client","message":"Client authentication failed"}
我已经尝试了几乎所有我在网上遵循的指南:,等等
但我仍然收到无效客户端
错误。以下是我通过邮递员传递给oauth/token的信息:
{
"grant_type": "password,"
"client_id": "3,"
"client_secret": "8BUPCSyYEdsgtZFnD6bFG6eg7MKuuKJHLsdW0k6g,"
"username": "test@gmail.com,"
"password": "123456,"
"scope": ""
}
任何帮助都将不胜感激。首先检查您的凭据是否正确,然后检查您的模型表,该表使用\Laravel\Passport\HasApiTokens
特性,即它是否包含电子邮件
列,因为默认情况下,它用于在验证凭据时识别用户。如果您的表具有用户名
>列或用于验证凭据的任何其他列您必须在该模型中定义函数findForPassport
public function findForPassport($username) {
return self::where('username', $username)->first(); // change column name whatever you use in credentials
}
我使用用户名和密码列验证用户,
在{project\u directory}\vendor\laravel\passport\src\Bridge\UserRepository.php中
此函数用于验证您的凭据
public function getUserEntityByUserCredentials($username, $password, $grantType, ClientEntityInterface $clientEntity)
{
if (is_null($model = config('auth.providers.users.model'))) {
throw new RuntimeException('Unable to determine user model from configuration.');
}
if (method_exists($model, 'findForPassport')) { // if you define the method in that model it will grab it from there other wise use email as key
$user = (new $model)->findForPassport($username);
} else {
$user = (new $model)->where('email', $username)->first();
}
if (! $user || ! $this->hasher->check($password, $user->password)) {
return;
}
return new User($user->getAuthIdentifier());
}
注意第二个if语句,您将了解那里发生了什么
希望有此帮助:)我也遇到了同样的问题,通过设置basic.auth标题,将client_id作为用户名,secret作为密码,解决了这个问题
然后它工作得很好。我成功地通过REST(类似邮递员的服务)获得了Laravel Passport-Password\u Grant\u令牌,而无需任何额外的控制器或中间工具。这就是我如何做到的
在laravel中安装默认的Auth脚手架
安装passport
通过身份验证脚手架注册用户
Goto休息或邮递员,或任何其他类似服务
在oauth_clients表中检查表单参数的数据
通过rest-through-Request\u-body\u-form\u-data-not-headers写入您的表单参数并向*/oauth/token发送POST请求(需要完整的url)
(需要的表单参数包括授权类型、客户id、客户机密、用户名和密码)
您应该返回一个具有4个键(令牌类型、到期时间、访问令牌和刷新令牌)的对象,状态为200 OK
如果到目前为止一切顺利
打开另一个窗口:
向/api/user发送GET请求(同样需要完整url)
在头文件中写入2个名称-值对(Accept=>application/json,Authorization=>Bearer-ACCESS\u-TOKEN\u-get\u-In\u-PREVIOUS\u请求(to/oauth/TOKEN)
应该就是这样。您应该从/api/user获得一个用户对象作为响应。尝试使用php artisan passport创建一个新的密码客户端:client--password
然后很可能不会为该密码客户端分配用户。编辑数据库行并向其中添加现有用户id
{
"grant_type": "password,"
"client_id": "" <- edited and added user id
"client_secret": "8BU......," <- new secret
"username": "test@gmail.com," <- email of above added user id
"password": "123456,"<- password
"scope": ""
}
{
“授权类型”:“密码”
“client_id”:“这可能有助于为您指明正确的方向,因为我们自己正在实现密码授予
1-在步骤4上,而不是运行以下操作:
php artisan passport:install
运行以下操作以创建密码授予客户端:
php artisan passport:client --password
上面的命令将给出一个后续问题,以设置密码授予客户端的名称。请确保在数据库中获取此新记录的id和机密
2-您的POSTMAN Post请求对象应包装在类似以下内容的对象中:
'form_params' => [
'grant_type' => 'password',
'client_id' => 'my_client-id',
'client_secret' => 'my_client-secret',
'username' => 'username or email', <-pass through variables or something
'password' => 'my-user-password', <- pass through variables or something
'scope' => '',
],
确保您传递的秘密与数据库中为该客户机列出的秘密相匹配
确保要传递的id匹配并且是整数数据类型
确保客户端有某种名称
在使用密码授权时,不要担心user_id列
确保路线正确
确保您输入的电子邮件(用户名)是数据库中用户表中的实际用户(或您自定义laravel 5.3系统以接受为用户表的任何表,我将添加一个链接,介绍如何自定义laravel 5.3系统以接受不同的用户表作为默认值(如果需要)
确保grant_类型拼写正确
确保您已设置为接受响应承载令牌(表示访问令牌和刷新令牌)
除此之外,我在上述答案中概述的初始步骤应该会让您回到正确的轨道上。这里还有其他人提到了一些类似的步骤,这些步骤很有帮助,但也解决了与单独问题相关的相同错误(我确信这是与特定系统设置相关的设置问题造成的)
不过,我希望这会有所帮助。在passport服务器上设置客户端时,请检查重定向/回调url。检查从中获取客户端ID和密码的条目,确保重定向url正确无误
我遇到了同样的错误,发现重定向URL不正确
它需要指向/回调路由
php artisan passport:client --password
{"error":"invalid_client","message":"Client authentication failed"}
"grant_type": "password,"
"client_id": "3,"
"client_secret": "8BUPCSyYEdsgtZFnD6bFG6eg7MKuuKJHLsdW0k6g,"
SELECT * FROM oauth_clients WHERE id = 3 AND secret = '8BUPCSyYEdsgtZFnD6bFG6eg7MKuuKJHLsdW0k6g' AND password_client=1;
php artisan migrate
php artisan passport:client --password
$request->request->add([
'client_id' => '2',
'client_secret' => 'm3KtgNAKxq6Cm1WC6cDAXwR0nj3uPMxoRn3Ifr8L',
'grant_type' => 'password',
'username' => 'support@eliteride.co.uk',
'password' => 'pass22'
]);
$tokenRequest = $request->create('/oauth/token', 'POST', $request->all());
$token = \Route::dispatch($tokenRequest);
$client_id = '111';
$client_secret = '1212';
protected $clientID = '578324956347895';
protected $clinetSecret ='hflieryt478';
public function index(Request $request)
{
if (!($request->input('clientID') && $request->input('clinetSecret'))) {
return false;
}
$project = User::all();
return $project
}
{
"grant_type": "password",
"client_id": 2,
"client_secret":
"username": ,
"password":
"scope": ""
}
$query = http_build_query([
'client_id' => 1,
'redirect_uri' => 'http://localhost:3000/callback',
'response_type' => 'code',
'scope' => '*'
]);