Laravel,Guzzle和Salesforce - 不支持的授权类型

I'm trying to get a token from Salesforce using Laravel and Guzzle. When trying through Postman, the login works fine. When I try my endpoint call, I get an unsupported_grant_type error.

Here is my code:

public function login(LoginRequest $request) {
    $client = new Client();

    $salesforce = new Request(
      'POST',
      'https://test.salesforce.com/services/oauth2/token',
      [
        'headers' => [
          'Content-Type' => 'application/x-www-form-urlencoded'
        ],
        'form_params' => [
          'username' => $request->email,
          'password' => $request->password,
          'client_id' => '<super_secret_id>',
          'client_secret' => '<super_secret_secret>',
          'grant_type' => 'password'
        ]
      ]);

    $response = $client->send($salesforce);
  }

This error message mostly means that they didn't find 'grant_type' parameter in your request.

It seems you're creating a request with Psr7\Request, 3rd parameter - headers. So, you're sending your parameters as a header.

Check this example:

public function login(LoginRequest $request) {
    $client = new Client();

    $response = $client->request(
        'POST',
        'https://test.salesforce.com/services/oauth2/token',
        [
            'headers' => [
                'Content-Type' => 'application/x-www-form-urlencoded'
            ],
            'form_params' => [
                'username' => $request->email,
                'password' => $request->password,
                'client_id' => '<super_secret_id>',
                'client_secret' => '<super_secret_secret>',
                'grant_type' => 'password'
            ]
        ]
    );

}