JSON ResponseTypeError - 'The "data" argument must be of type string'

JSON response TypeError - 'The "data" argument must be of type string'

我正在尝试获取访问令牌的 API 为我提供了以下说明 (您可以在 https://developers.finove.com.br/authentication 自行检查):


Use your 'ClientId' and 'ClientSecret' to fetch an access token and the API authentication follows standard Oauth 2.0 protocols.

The API only accepts requests in JSON, so all requests must include the header 'Content-Type: application/json'.

正文参数:

clientIdclientSecret 作为字符串

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

{

accessToken:“eyJhbGciOiJSUz...”

}


对,所以我使用以下函数 POST 向 API 发出 JSON 请求,获取响应并获取 '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()。使用 Insomnia 应用程序:https://insomnia.rest 检查 API 的 JSON 响应也确实有帮助。