今天早上,我访问 Ebay API 时遇到错误,提示我的访问令牌无效。自从我最初生成它以来已经有一段时间了(可能是 18 个月 - 遗憾的是我没有得到 Ebay 的实际回复),所以我决定更新/续订刷新令牌,这应该会再持续 18 个月。
我发现了这个并完成了这些步骤: https://developer.ebay.com/api-docs/static/oauth-consent-request.html
所以我生成了 URL 并将其复制到我的浏览器中: https://auth.ebay.com/oauth2/authorize?client_id=CLIENTID&redirect_uri=RUNAME&response_type=code&scope=https://api.ebay.com/oauth/api_scope...
我在浏览器中批准了请求并复制了生成的 URL: https://signin.ebay.com/ws/eBayISAPI.dll?ThirdPartyAuthSucessFailure&isAuthSuccessful=true&code=v%5E1.1%23...2MA%3D%3D&expires_in=299
然后我在 Postman 中创建了帖子(cURL 示例):
curl_setopt_array($curl, array(
CURLOPT_URL => 'https://api.ebay.com/identity/v1/oauth2/token',
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => '',
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 0,
CURLOPT_FOLLOWLOCATION => true,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => 'POST',
CURLOPT_POSTFIELDS => 'grant_type=authorization_code&code=v%5E1.1%23...2MA%3D%3D&redirect_uri=RUNAME',
CURLOPT_HTTPHEADER => array(
'Content-Type: application/x-www-form-urlencoded',
'Authorization: Basic BASE64(CLIENTID:SECRET)',
'Cookie: dp1=bu1p/QEBfX0BAX19AQA**6608a38c^'
),
));
我在代码有效的 5 分钟内执行此操作。我收到的只是400 Bad Request。我尝试按照从 Ebay 返回的确切方式发送代码,以及对其进行解码并以这种方式发送。
我的问题与使用 Postman 发送请求有关。看来,由于各种设置,Postman 在不应该的情况下对项目进行了 URL 编码。因此,我一时兴起尝试通过 PHP 来运行调用,效果很好。
$curl = curl_init();
$data = [
'grant_type' => "authorization_code",
'redirect_uri' => "RUNAMEHERE,
'code' => urldecode('v%5E1.1%23i%5...VeMjYw') // from grant URL call
];
curl_setopt_array(
$curl,
[
CURLOPT_URL => 'https://api.ebay.com/identity/v1/oauth2/token',
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => '',
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 0,
CURLOPT_FOLLOWLOCATION => true,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => 'POST',
CURLOPT_POSTFIELDS => http_build_query($data),
CURLOPT_HTTPHEADER => [
'Content-Type: application/x-www-form-urlencoded',
'Authorization: Basic ' . base64_encode($config['app_id'] . ":" . $config['cert_id'])
]
]
);
$response = curl_exec($curl);
$status = curl_getinfo($curl);
curl_close($curl);
$results = json_decode($response, true);
print_r($results);
print_r($status);
希望能帮助别人。