You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用eBay API通过UPC匹配目录时获Access Token遇invalid_scope错误求助

问题描述

我尝试通过UPC在eBay目录中搜索匹配商品,用《生活大爆炸》第一季DVD的UPC(9325336046425)测试(该UPC在生产环境目录中存在),但始终无法获取Access Token,报错信息如下:
**Failed to get access token: 400
{'error': 'invalid_scope', 'error_description': 'The requested scope is invalid, unknown, malformed, or exceeds the scope granted to the client'}
**

测试代码:

import base64

# Your eBay sandbox credentials
credentials = {
    "client_id": "[ ]",
    "client_secret": "[ ]"
}

# Token endpoint
token_url = "https://api.sandbox.ebay.com/identity/v1/oauth2/token"

# Encode client ID and secret for Basic Authentication
auth_header = base64.b64encode(
    f"{credentials['client_id']}:{credentials['client_secret']}".encode()
).decode()

# Request headers and payload for the token
headers = {
    "Authorization": f"Basic {auth_header}",
    "Content-Type": "application/x-www-form-urlencoded"
}
data = {
    "grant_type": "client_credentials",
    "scope": "https://api.ebay.com/oauth/api_scope/commerce.catalog.readonly"
}

# Step 1: Obtain the Access Token
response = requests.post(token_url, headers=headers, data=data)
if response.status_code == 200:
    access_token = response.json().get("access_token")
    if not access_token:
        print("Error: Access token not found in the response.")
        print(response.json())
        exit()
    print("Access token obtained successfully.")
else:
    print(f"Failed to get access token: {response.status_code}")
    print(response.json())
    exit()

# Step 2: Search for the UPC in the eBay Catalog
catalog_search_url = "https://api.sandbox.ebay.com/commerce/catalog/v1_beta/product_summary/search"
search_headers = {
    "Authorization": f"Bearer {access_token}",
    "Content-Type": "application/json",
    "Accept": "application/json"
}
params = {
    "q": "9325336046425"  # UPC to search for
}

search_response = requests.get(catalog_search_url, headers=search_headers, params=params)

# Check the response
if search_response.status_code == 200:
    print("Search response:")
    print(search_response.json())
elif search_response.status_code == 404:
    print("No results found for the provided UPC.")
else:
    print(f"Failed to search for UPC: {search_response.status_code}")
    print(search_response.json())
解决方法

1. 核心原因

invalid_scope错误的本质是:请求的权限范围与你的eBay开发者账号授权的权限不匹配,或应用未开通对应API权限。

2. 修复步骤

(1)确认应用权限配置

登录eBay开发者平台,进入你的沙箱应用详情页:

  • 找到「API Permissions」板块
  • 确认已添加Commerce Catalog API的只读权限;若未添加,点击「Add API Permission」,勾选该API后保存

(2)修正代码遗漏

原代码缺少requests库导入,运行时会直接报错,必须补上:

import requests

(3)验证Scope正确性

确保scope参数完全正确,无拼写错误:

"scope": "https://api.ebay.com/oauth/api_scope/commerce.catalog.readonly"

3. 修正后的完整代码

import base64
import requests

# 替换为你的沙箱凭证
credentials = {
    "client_id": "你的沙箱Client ID",
    "client_secret": "你的沙箱Client Secret"
}

token_url = "https://api.sandbox.ebay.com/identity/v1/oauth2/token"

# 生成Basic认证头
auth_header = base64.b64encode(
    f"{credentials['client_id']}:{credentials['client_secret']}".encode()
).decode()

headers = {
    "Authorization": f"Basic {auth_header}",
    "Content-Type": "application/x-www-form-urlencoded"
}
data = {
    "grant_type": "client_credentials",
    "scope": "https://api.ebay.com/oauth/api_scope/commerce.catalog.readonly"
}

# 获取Access Token
response = requests.post(token_url, headers=headers, data=data)
if response.status_code == 200:
    access_token = response.json().get("access_token")
    if not access_token:
        print("错误:响应中未找到Access Token")
        print(response.json())
        exit()
    print("Access Token获取成功")
else:
    print(f"获取Access Token失败:{response.status_code}")
    print(response.json())
    exit()

# 搜索UPC
catalog_search_url = "https://api.sandbox.ebay.com/commerce/catalog/v1_beta/product_summary/search"
search_headers = {
    "Authorization": f"Bearer {access_token}",
    "Content-Type": "application/json",
    "Accept": "application/json"
}
params = {
    "q": "9325336046425"  # 目标UPC
}

search_response = requests.get(catalog_search_url, headers=search_headers, params=params)

if search_response.status_code == 200:
    print("搜索结果:")
    print(search_response.json())
elif search_response.status_code == 404:
    print("未找到该UPC对应的商品")
else:
    print(f"搜索UPC失败:{search_response.status_code}")
    print(search_response.json())

4. 额外注意

  • 沙箱环境商品数据与生产环境隔离,即使生产环境存在该UPC,沙箱中可能无对应数据,若获取Token后搜索返回404,可更换沙箱内存在的UPC测试
  • 确保使用的是沙箱环境的Client ID和Client Secret,不要混用生产环境凭证

内容的提问来源于stack exchange,提问作者Sasha Gorgov

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.14 23:37:04