JSON 响应类型错误 - ""data"参数必须是字符串类型"



我试图获取访问令牌的API给了我以下说明(您可以在https://developers.finove.com.br/authentication检查自己):


使用您的'ClientId'和'ClientSecret'来获取访问令牌,API认证遵循标准Oauth 2.0协议。

API只接受JSON格式的请求,所以所有的请求必须包含头'Content-Type: application/JSON '。

clientIdclientSecret作为字符串

响应200将包含如下访问令牌:

{

accessToken:"eyJhbGciOiJSUz…">

}


对,所以我使用以下函数将JSON请求POST到API,获得响应并获取'acessToken'值。

<?php
// ignore the $order because its not used in the function, just passed
private function finove_auth($order){ 

$authurl = 'https://api.finove.com.br/api/auth/authenticate';
$client_id = $this->apiId;
$client_secret = $this->apiSecret; 
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $authurl);
curl_setopt($ch, CURLOPT_POST, TRUE);
curl_setopt($ch, CURLOPT_HTTPHEADER, 'Content-Type: application/json');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_POSTFIELDS, array(
'clientId'      => $client_id,
'clientSecret'  => $client_secret,
));
$data = curl_exec($ch);
if ( curl_errno( $ch ) ){
// for printing on console and allowing me to check whats happening
echo 'Error: ' . curl_error( $ch );
return $this->finove_payment_processing( $order );
}
else {
curl_close($ch);
$auth_string = json_decode($data, true);
print_r($auth_string); // to check the response on the console
$this->finove_payment_processing( $order );
}        
}

但是有些东西不工作。在控制台上它返回给我两个东西。首先它

Fixed malformed JSON. Original:

,第二个是:


array(7) {
["name"]=>
string(9) "TypeError"
["message"]=>
string(112) "The "data" argument must be of type string or an instance of Buffer, TypedArray, or DataView. Received undefined"
["errors"]=>
array(0) {
}
["table"]=>
string(0) ""
["constraint"]=>
string(0) ""
["paramName"]=>
string(0) ""
["stack"]=>
string(585) "TypeError [ERR_INVALID_ARG_TYPE]: The "data" argument must be of type string or an instance of Buffer, TypedArray, or DataView. Received undefined
at Hmac.update (internal/crypto/hash.js:84:11)
at ApiKeyService.verify (/usr/src/app/dist/api/services/ApiKeyService.js:15:18)
at AuthController.<anonymous> (/usr/src/app/dist/api/controllers/AuthController.js:17:48)
at Generator.next (<anonymous>)
at fulfilled (/usr/src/app/node_modules/tslib/tslib.js:114:62)
at runMicrotasks (<anonymous>)
at processTicksAndRejections (internal/process/task_queues.js:95:5)"
}

不能弄清楚我做错了什么,但似乎clientId和ClientSecret没有被发送,因为他们应该。

下面的代码可以工作:

<?php
private function finove_auth(){

// Get the id and secret values
$clientId = $this->finoveID; 
$clientSecret =  $this->finoveSecret;       
$url = "https://api.finove.com.br/api/auth/authenticate";
// encode to json
$data = array("clientId" => "$clientId", "clientSecret" => "$clientSecret");                                                                    
$data_string = json_encode($data);   
// begin the request
$curl = curl_init();
curl_setopt_array($curl, [ // request properties
CURLOPT_URL => $url,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => "",
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 30,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => "POST",
CURLOPT_POSTFIELDS => $data_string,
CURLOPT_HTTPHEADER => [
"Content-Type: application/json"
],
]);

$response = curl_exec($curl); // execute the request
$err = curl_error($curl); // get error if occur

curl_close($curl); // close the request

if ($err) {
print_r("cURL Error #:" . $err); // print error to log
} else {
print_r($response); // print value to log
}

}

似乎问题在于我没有将信息放入数组中并正确使用Json_decode()。使用失眠应用:https://insomnia.rest来检查API的JSON响应也很有帮助。

相关内容

最新更新