我的代码如下所示。我正在尝试使用eBay对用户进行身份验证,以便能够获取访问令牌。在将用户重定向回我的重定向URL以使用访问令牌交换代码时,我收到错误:
HTTP / 1.1 100继续HTTP / 1.1 400错误请求内容长度:109 Cneonction:close日期:星期四,2017年3月2日06:47:51 GMT RlogId: t6ldssk%28ciudbq%60anng%7Fu2h%3F%3Cwk%7Difvqn * 2%3D%3F%3E505-15a8dc659e6-0xf3 Set-Cookie:ebay =%5Esbf%3D%23%5E; Domain = .ebay.com; Path = / X-EBAY-C-REQUEST-ID:ri = v22kQg9yaBLq,rci = 2FDiyansIYnkONQC X-EBAY-C-VERSION:1.0.0 X-EBAY-REQUEST-ID: 15a8dc658f8.a0968eb.5f6ef.fff87b7e![] Content-Type:application / json 连接:保持活力 {"错误":" INVALID_REQUEST"" ERROR_DESCRIPTION":"'授权'是 错过了标题:"," error_uri":null}
我的POST请求有什么问题?
$auth_code = $_GET['code'];
$client_id = "Client ID";
$client_secret = "Client Secret";
$redirect_uri = "RuName";
$headers = array (
'Content-Type' => 'application/x-www-form-urlencoded',
'Authorization' => sprintf('Basic %s',base64_encode(sprintf('%s:%s', $client_id, $client_secret)))
);
$apiURL = "https://api.sandbox.ebay.com/identity/v1/oauth2/token";
$urlParams = array (
"grant_type" => "authorization_code",
"code" => urlencode($auth_code),
"redirect_uri" => $redirect_uri
);
$ch = curl_init();
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
//curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); // Should be removed on production
curl_setopt ( $ch, CURLOPT_POST, 1 );
curl_setopt ( $ch, CURLOPT_HEADER, true );
curl_setopt($ch, CURLOPT_URL, $apiURL);
curl_setopt ( $ch, CURLOPT_HTTPHEADER, $headers = array ('Authorization' => sprintf('Basic %s',base64_encode(sprintf('%s:%s', $client_id, $client_secret))),'Content-Type' => 'application/x-www-form-urlencoded') );
curl_setopt ( $ch, CURLOPT_POSTFIELDS, $urlParams );
$resp = curl_exec ( $ch );
curl_close ( $ch );
print_r ( $resp );
答案 0 :(得分:2)
您的代码存在两个小问题。第一个是CURLOPT_HTTPHEADER需要一个字符串数组而不是一个关联数组。
$headers = array (
'Content-Type: application/x-www-form-urlencoded',
'Authorization: '.sprintf('Basic %s',base64_encode(sprintf('%s:%s', $client_id, $client_secret)))
);
第二个是如何处理PHP处理CURLOPT_POSTFIELDS选项。正如documentation中所述。
将数组传递给CURLOPT_POSTFIELDS会将数据编码为 multipart / form-data,传递URL编码的字符串将进行编码 数据为application / x-www-form-urlencoded。
由于eBay API要求正文为application/x-www-form-urlencoded
,因此您需要传入字符串。您可以使用数组通过将其传递给http_build_query来构建此字符串。
$urlParams = http_build_query(array (
"grant_type" => "authorization_code",
"code" => $auth_code,
"redirect_uri" => $redirect_uri
));
我还建议Postman测试向eBay发出API请求。其中一个功能是它还将为您创建PHP卷曲代码。下面的代码改编自Postman创建的代码。
<?php
$clientID = '<YOUR EBAY APP ID>';
$clientSecret = '<YOUR EBAY CERT ID>';
$ruName = '<YOUR EBAY RUNAME>';
$authCode = '<AUTH CODE>';
$url = 'https://api.sandbox.ebay.com/identity/v1/oauth2/token';
$headers = [
'Content-Type: application/x-www-form-urlencoded',
'Authorization: Basic '.base64_encode($clientID.':'.$clientSecret)
];
$body = http_build_query([
'grant_type' => 'authorization_code',
'code' => $authCode,
'redirect_uri' => $ruName
]);
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_URL => $url,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_CUSTOMREQUEST => 'POST',
CURLOPT_POSTFIELDS => $body,
CURLOPT_HTTPHEADER => $headers
));
$response = curl_exec($curl);
$err = curl_error($curl);
curl_close($curl);
if ($err) {
echo "cURL Error #:" . $err;
} else {
echo $response."\n";
}