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

时间:2017-01-16 19:12:28

标签: php laravel salesforce guzzle guzzle6

我试图使用Laravel和Guzzle从Salesforce获取令牌。通过Postman尝试时,登录工作正常。当我尝试端点调用时,出现了unsupported_grant_type错误。

这是我的代码:

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);
  }

1 个答案:

答案 0 :(得分:4)

此错误消息通常意味着他们没有找到“grant_type”#39;请求中的参数。

您似乎正在使用Psr7 \ Request,第三个参数 - 标头创建请求。因此,您需要将参数作为标题发送。

检查此示例:

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'
            ]
        ]
    );

}