0

我想使用 finerworks api,我的 curl 代码在这里

$data1 = array(
'web_api_key' => '********-****-****-*****-************',
'app_key' => '********-****-****-*****-************',
);
$data2['credentials'] = $data1;
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_URL => "https://api.finerworks.com/v3/test_my_credentials",
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => "",
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 30000,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => "POST",
CURLOPT_POSTFIELDS => json_encode($data2),
CURLOPT_HTTPHEADER => array(
    "accept: */*",
    "accept-language: en-US,en;q=0.8",
    "content-type: application/json",
),
));

$response = curl_exec($curl);
$err = curl_error($curl);

curl_close($curl);

if ($err) {
echo "cURL Error #:" . $err;
} else {
print_r(json_decode($response));
}

当我在这里运行此代码时,下面是我的请求响应

stdClass Object ( [Message] => The request is invalid. [ModelState] => stdClass Object ( [ApiC.Models.authorization_credentials] => Array ( [0] => Missing or unauthorized api credentials ) ) ) 

请帮我解决这个问题提前谢谢

4

1 回答 1

2

在标头中传递凭据

$data1 = array(
    'web_api_key' => '********-****-****-*****-************',
    'app_key' => '********-****-****-*****-************',
);
$data2['credentials'] = $data1;
$curl = curl_init();
curl_setopt_array($curl, array(
    CURLOPT_URL => "https://api.finerworks.com/v3/test_my_credentials",
    CURLOPT_RETURNTRANSFER => true,
    CURLOPT_ENCODING => "",
    CURLOPT_MAXREDIRS => 10,
    CURLOPT_TIMEOUT => 30000,
    CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
    CURLOPT_CUSTOMREQUEST => "POST",
    CURLOPT_POSTFIELDS => json_encode($data2),
    CURLOPT_HTTPHEADER => array(
        "accept: */*",
        "accept-language: en-US,en;q=0.8",
        "content-type: application/json",
        "web_api_key: YOUR web_api_key",
        "app_key: YOUR app_key",
    ),
));

$response = curl_exec($curl);
$err = curl_error($curl);

curl_close($curl);

if ($err) {
    echo "cURL Error #:".$err;
} else {
    print_r(json_decode($response));
}
于 2020-11-09T17:18:31.633 回答