Guzzle - 400 Bad Request` response:{“error”:“invalid_client”} - 制作令牌请求时
问题描述:
我试图使用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编码的客户端机密}
不知道在哪里,我出错了。
答
我已经完成了。答案是如下:
<?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;
尝试检查发送到API服务器的请求字符串之前,它得到内发送到API服务器还有那些外壳有可能是出事的字符串[和]是那些刚表示或者是如何在代码中实现它们? –