我試圖獲取訪問令牌的 API 為我提供了以下說明(您可以在https://developers.finove.com.br/authentication 自行檢查):
使用您的“ClientId”和“ClientSecret”獲取訪問令牌,API 身份驗證遵循標準 Oauth 2.0 協議。
API 只接受 JSON 格式的請求,因此所有請求都必須包含標頭“Content-Type: application/json”。
引數在body上:
clientId和clientSecret作為字串
回應 200 將包含訪問令牌,如下所示:
{
訪問令牌:“eyJhbGciOiJSUz...”
}
對,所以我使用以下函式向 API 發布 JSON 請求,獲取回應并獲取“accessToken”值。
<?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 沒有按應有的方式發送。
uj5u.com熱心網友回復:
以下代碼有效:
<?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 回應也確實有幫助。
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/475682.html
下一篇:我在嘗試運行此代碼時遇到此問題(gapi.auth2.getAuthInstance().signIn();)"idpiframe_initialization_failed"
