Laravel 8-unsupported_grant_type

问题描述

我有以下问题:

  • 我正在创建一个API(这是我的新手)
  • 使用这个API,我想使用另一个API或WebService
  • 在邮递员中,我对URL进行了获取请求,并向其返回了确定的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 **,但是我现在不使用数据库

解决方法

问题是x-www-form-urlencoded。

这解决了我的问题(asForm)

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

dd($response->json());