【问题标题】:Laravel, Guzzle, and Salesforce - Unsupported Grant TypeLaravel、Guzzle 和 Salesforce - 不支持的授权类型
【发布时间】:2017-05-31 17:02:21
【问题描述】:

我正在尝试使用 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);
  }

【问题讨论】:

  • 什么版本的 Guzzle?
  • 6.2 版是引入的。
  • 您是否阅读过 Guzzle 6+ 的文档?
  • 是的,这就是我所关注的。我在想也许有一些特定的赠款类型,但我什么也没看到。
  • 如果 Request 是 Psr7\Request,你应该把 headers 和 body 分开。新请求('POST', '...', $headers, $body);

标签: php laravel salesforce guzzle guzzle6


【解决方案1】:

此错误消息主要表示他们没有在您的请求中找到“grant_type”参数。

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

检查这个例子:

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

}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-05-14
    • 2017-07-27
    • 2021-09-20
    • 2017-09-18
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多