集成OAuth与eBay Browse API替换关停的eBay Finding API实现商品展示
你的实现思路完全可行,使用客户端凭证模式获取应用访问令牌调用eBay Browse API的方案符合官方要求,问题出在OAuth流程的代码细节错误,修正后即可正常运行。
现有代码的核心错误
1. getOAuthCreds 函数问题
- POST请求参数格式错误:客户端凭证模式的请求体需要为urlencoded格式,
grant_type和scope参数之间缺少&连接符,且Content-Type头错误设置为application/json,实际应为application/x-www-form-urlencoded - 认证头格式错误:获取令牌的请求不需要传Bearer令牌,而是需要将你eBay开发者后台的Client ID和Client Secret以
client_id:client_secret格式拼接后做Base64编码,最终请求头格式为Authorization: Basic 编码后的字符串 - 返回值未解析:OAuth接口返回的是包含access_token的JSON字符串,需要解析提取令牌字段后再返回,不能直接返回完整响应
2. Browse API调用问题
- 认证头拼接错误:缺少
Bearer前缀,且字符串拼接缺少PHP连接符.,正确格式应为"Authorization: Bearer " . getOAuthCreds()
修正后参考代码
<?php /* Template Name: XXXX */ ini_set('display_errors', 1); ini_set('display_startup_errors', 1); error_reporting(E_ALL); // 替换为你自己的eBay开发者凭证 define('EBAY_CLIENT_ID', '你的Client ID'); define('EBAY_CLIENT_SECRET', '你的Client Secret'); // 令牌缓存时间:7200秒=2小时,和eBay官方有效期一致 define('EBAY_TOKEN_CACHE_TIME', 7200); function getOAuthCreds() { // 先读缓存,避免重复请求 $cached_token = get_option('ebay_access_token'); $cached_expire = get_option('ebay_token_expire'); if ($cached_token && time() < $cached_expire) { return $cached_token; } $endpoint = 'https://api.ebay.com/identity/v1/oauth2/token'; // 参数拼接加& $request = "grant_type=client_credentials&scope=https://api.ebay.com/oauth/api_scope"; // 生成Basic认证头 $basic_auth = base64_encode(EBAY_CLIENT_ID . ':' . EBAY_CLIENT_SECRET); $session = curl_init($endpoint); curl_setopt($session, CURLOPT_POST, true); curl_setopt($session, CURLOPT_POSTFIELDS, $request); curl_setopt($session, CURLOPT_RETURNTRANSFER, true); $headers = [ 'Content-Type: application/x-www-form-urlencoded', 'Authorization: Basic ' . $basic_auth ]; curl_setopt($session, CURLOPT_HTTPHEADER, $headers); $response = curl_exec($session); curl_close($session); // 解析响应提取令牌 $result = json_decode($response, true); if (isset($result['access_token'])) { // 写入缓存 update_option('ebay_access_token', $result['access_token']); update_option('ebay_token_expire', time() + $result['expires_in']); return $result['access_token']; } return false; } $access_token = getOAuthCreds(); if (!$access_token) { echo "获取认证令牌失败,请检查开发者凭证配置"; exit; } $curl = curl_init(); curl_setopt_array($curl, array( CURLOPT_URL => "https://api.ebay.com/buy/browse/v1/item_summary/search?q=iphone&sort=-price", CURLOPT_RETURNTRANSFER => true, CURLOPT_FOLLOWLOCATION => true, CURLOPT_ENCODING => "", CURLOPT_MAXREDIRS => 10, CURLOPT_TIMEOUT => 30, CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1, CURLOPT_CUSTOMREQUEST => "GET", CURLOPT_HTTPHEADER => array( "Content-Type:application/json", "Authorization: Bearer " . $access_token, "X-EBAY-C-MARKETPLACE-ID:EBAY_US", "X-EBAY-C-ENDUSERCTX:affiliateCampaignId=xx,affiliateReferenceId=xx", ), )); $response = curl_exec($curl); $err = curl_error($curl); curl_close($curl); // 后续业务逻辑保持不变即可
额外注意事项
- 替换代码中
你的Client ID和你的Client Secret为你eBay开发者后台对应应用的凭证 - 原有代码中搜索接口的
sort参数为空,可根据需求补全排序规则,示例中已补全为按价格倒序作为参考
内容的提问来源于stack exchange,提问作者John Rogerson
相关产品推荐
相关产品推荐

