21

我已经按照文档安装了 Laravel Passport,并且修改了所有需要的代码区域。

我正在设置密码授予令牌,以便用户在使用网站的用户名和密码登录时能够获得 API 令牌。不过,当涉及到 grant_type 时,我遇到了一个问题。出于某种原因,Laravel 抱怨授权类型无效。

{
  "error": "unsupported_grant_type",
  "message": "The authorization grant type is not supported by the authorization server.",
  "hint": "Check the `grant_type` parameter"
}

这些是我发布到的字段/oauth/token

client_id = 4
client_secret = SMiYE7XqDNtXKQnmkYmFnXxfAaV83vRhnJ9zwCtZ
username = jcrawford@domain.com
password = **************
grant_type = password
scope = *

我跑过php artisan passport:install,我也试过跑步php artisan passport:client --password

这两个命令都有效并且都创建了客户端和机密,但是,我似乎无法克服有关 grant_type 的错误。

关于我应该看什么来解决这个问题的任何建议,以便密码授予令牌对我有用?

4

5 回答 5

43

看来您必须将参数作为表单数据发送,而不是像我正在做的那样在标题中发送......新手错误!

于 2016-12-30T16:13:30.353 回答
9

我正在使用 Postman,并且我已将所有参数放在 Params 中。邮递员显示以下响应

{
    "error": "unsupported_grant_type",
    "message": "The authorization grant type is not supported by the authorization server.",
    "hint": "Check the `grant_type` parameter"
}

现在我将所有参数都放在 Body 中,然后按下 Send 按钮,效果很好。

于 2018-10-25T10:25:23.357 回答
1

对我来说,问题是我没有使用Request $request,我使用的是RegisterRequest我创建的 $request。

于 2020-05-14T04:17:15.153 回答
0

阅读 Laravel 文档为我减轻了很多压力。用于使用指定的oauth\token授权类型检索令牌,路由将返回包含 access_token、refresh_token 和 expires_in 属性的 JSON 响应。expires_in 属性包含访问令牌过期前的秒数(参考

  1. 安装护照
  2. 发布服务提供者和迁移并迁移。
  3. 设置登录/注册路线以创建帐户并登录。
  4. 在您的用户模型中,HasApiTokens从使用 Laravel\Passport\HasApiTokens 添加;
  5. 在登录方法的响应中,将令牌添加为响应的一部分。 在此处输入图像描述
  6. 测试你对邮递员的回应 在此处输入图像描述
于 2021-12-23T16:11:59.010 回答
0

初始网址

https://restfulapi.test/oauth/authorize?client_id=3&redirect_url=http://restfulapi.test?response_type=code

解决方案

https://restfulapi.test/oauth/authorize?client_id=3&redirect_url=http://restfulapi.test&response_type=code

我必须将 response_type 之前的问号替换为&

于 2018-05-07T13:56:00.350 回答