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

Time:05-17

The API I'm trying to fetch an access token gives me the following instructions (you can check for yourself at 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'.

The Parameters goes on thebody:

clientId and clientSecret as a string

The Response 200 will contain the acess Token as followed:

{

accessToken: "eyJhbGciOiJSUz..."

}


Right, so I using the following function to POST a JSON request to the API, get the response and fetch the 'acessToken' value.

<?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 );
        }        
}

But something not working. On the console its returning me two things. Firts its

Fixed malformed JSON. Original:

and second is:


    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)"
    }

Can't figure it out what I'm doing wrong but it seens that the clientId and ClientSecret are not beeing send as they should.

CodePudding user response:

The following code works:

<?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
    }
  
}

It seens that the problem was that I was not putting the infos inside a array and using Json_decode() properly. Using the Insomnia app: https://insomnia.rest to check the JSON response from the API really helped aswell.

  • Related