Guzzle - 400 Bad Request`回复:{"错误":" invalid_client"} - 在提出令牌请求时

时间:2017-10-28 15:19:53

标签: php guzzle

我尝试使用guzzle发出令牌请求并收到错误" 400 Bad Request`回复:{"错误":" invalid_client"}& #34 ;.我可以使用cURL和HTTP_Request2发出相同的请求,没有任何问题。

<?php
    require 'vendor/autoload.php';
    use GuzzleHttp\Client;
    use GuzzleHttp\Exception\RequestException;
    use GuzzleHttp\Psr7\Request;

    session_start();
    if(isset($_GET['code'])){
        $code = $_GET['code'];
        $encodeB64 = base64_encode('{clientID}:{clientSecret}');
        $client = new GuzzleHttp\Client();
        $response = $client->request('POST', 'https://identity.reckon.com/connect/token',[
        ['headers' => ['Content-Type' => 'application/x-www-form-urlencoded'],['Authorization' => 'Basic '.$encodeB64]],
        ['body' => ['grant_type' => 'authorization_code'],['code' => $code],['redirect_uri' => '{redirectURI}']]
        ]);
        $body = $response->getBody();
        echo $body;
    }

以下是有关如何使用此API发出令牌请求的详细信息:

网址:https://identity.reckon.com/connect/token

键入:POST

正文:grant_type = authorization_code&amp; code = {code}&amp; redirect_uri = {redirect url}

接头:

  • Content-Type = application / x-www-form-urlencoded

  • 授权:基本{客户端ID:以base64编码的客户端密码}

不确定我哪里出错了。

2 个答案:

答案 0 :(得分:0)

我已经解决了。答案如下:

<?php
    require 'C:/Users/Shane/vendor/autoload.php';
    use GuzzleHttp\Client;
    use GuzzleHttp\Exception\RequestException;
    use GuzzleHttp\Psr7\Request;

    session_start();
    if(isset($_GET['code'])){
        $code = $_GET['code'];
        $encodeB64 = base64_encode('{client id}:{client secret}');
        $authbody = 'grant_type=authorization_code&code='.$code.'&redirect_uri={redirect url}';
        $client = new GuzzleHttp\Client();
        $response = $client->request('POST', 'https://identity.reckon.com/connect/token',['headers' => 
            ['Content-Type' => 'application/x-www-form-urlencoded','Authorization' => 'Basic '.$encodeB64],
            'body' => $authbody]);
        $body = $response->getBody();
        echo $body;

答案 1 :(得分:0)

我最近与Guzzle一起经历了{"error":"invalid_client"},该错误实际上专门告诉您clientIdclientSecret是否出了问题。就我而言,我的首字母clientSecret大写。花了一段时间才弄清楚。