我有我的代码如下所示。我试图用eBay对用户进行身份验证,以获得访问令牌。在将用户重定向到我的重定向URL以使用访问令牌交换代码时,我会得到以下错误:
HTTP/1.1 100继续HTTP/1.1 400不良请求内容-长度: 109 Content:截止日期:清华,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 C-请求-ID: ri=v22kQg9yaBLq,rci=2FDiyansIYnkONQC X-EBAY-C-版本: 1.0.0 X-EBAY-请求-ID: 15a8dc658f8.a0968eb.5f6ef.fff87b7e![]内容-类型:应用程序/json连接:保持-活动{“错误”:“invalid_request”,“error_description”:“授权”缺少标题:"error_uri":null}
我的邮件请求有什么问题?
$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 );发布于 2017-03-03 11:07:59
您的代码有两个小问题。首先,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选项有关。正如在文档中所指出的。
将数组传递给CURLOPT_POSTFIELDS将数据编码为多部分/表单-数据,而传递URL编码的字符串将将数据编码为application/x form-urlencoded。
由于eBay API要求主体为application/x-www-form-urlencoded,因此需要传递一个字符串。通过将数组传递给查询,您可以使用数组来构建该字符串。
$urlParams = http_build_query(array (
"grant_type" => "authorization_code",
"code" => $auth_code,
"redirect_uri" => $redirect_uri
));我还建议邮递员测试如何向eBay发出API请求。它的一个特性是它还将为您创建PHP curl代码。下面的代码是从邮递员创建的代码中改编的。
<?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";
}https://stackoverflow.com/questions/42549023
复制相似问题