【问题标题】:Laravel 8 - unsupported_grant_typeLaravel 8 - unsupported_grant_type
【发布时间】:2020-09-12 01:20:20
【问题描述】:

我有以下问题:

  • 我正在创建一个 API(我是新手)
  • 有了这个 API,我想使用另一个 API 或 WebService
  • 在 Postman 中我向 URL 发出 get 请求,它返回 OK 的 access_token,但在 Laravel 项目中它返回:"error" => "unsupported_grant_type".

这是我的代码:

use Illuminate\Support\Facades\Http;

$response = Http::withHeaders([
            'content-type' => 'application/x-www-form-urlencoded'
        ])->post('http://url:52904/Token', [
            'userName' => 'menganito',
            'password' => 'pepito',
            'grant_type' => 'password'
        ]);

        dd($response->json());

我尝试安装 ** Laravel Passport **,但我暂时没有使用数据库。

【问题讨论】:

  • 你在postman中发送的参数是什么,消耗的是什么api?
  • 这更像是OAuth2 Grant Type 问题,而不是 Laravel 问题。请使用 Postman 使用相同的grant_type 和数据进行 POST 请求,看看它是否返回相同的结果。如果是这样,您使用的 API 根本不支持该授权类型(如错误提示)。
  • 在邮递员中我发送 x-www-form-urlencoded 但如果我选择 form-params 返回 "error" => "unsupported_grant_type"

标签: php laravel http http-post laravel-8


【解决方案1】:

问题是 x-www-form-urlencoded。

这解决了我的问题(asForm)

$response = Http::asForm()->post('http://url.com', [
                'userName' => 'user',
                'password' => 'pass',
                'grant_type' => 'password'
        ]);

dd($response->json());

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-10-10
    • 1970-01-01
    • 2017-08-29
    • 2012-12-31
    • 1970-01-01
    • 2016-06-22
    • 2019-04-22
    相关资源
    最近更新 更多