2017-10-28 234 views
1

我試圖使用guzzle做出令牌請求並收到錯誤「400 Bad Request」響應:{「error 「:」 invalid_client 「}」。我可以用cURL和HTTP_Request2進行同樣的請求,沒有任何問題。Guzzle - 400 Bad Request` response:{「error」:「invalid_client」} - 製作令牌請求時

<?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 &碼= {代碼} & redirect_uri = {redirect url}

插頭:

  • 的Content-Type =應用程序/ x-WWW窗體-urlencoded

  • 授權:基本{客戶ID:在base64編碼的客戶端機密}

不知道在哪裏,我出錯了。

+0

嘗試檢查發送到API服務器的請求字符串之前,它得到內發送到API服務器還有那些外殼有可能是出事的字符串[和]是那些剛表示或者是如何在代碼中實現它們? –

回答

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; 
相關問題