# 1. 添加时间戳 params[‘timestamp’] = time.strftime(‘%Y-%m-%d %H:%M:%S’, time.localtime())
2. 按参数名ASCII码升序排序
sorted_params = sorted(params.items())
3. 拼接参数
param_str = ‘&’.join([f”{k}={urllib.parse.quote(str(v))}” for k, v in sorted_params])
4. 生成签名:app_secret + 参数串 + app_secret
sign_str = f”{app_secret}{param_str}{app_secret}”
5. HMAC-SHA1加密并转大写
signature = hmac.new(
app_secret.encode('utf-8'), sign_str.encode('utf-8'), hashlib.sha1
).hexdigest().upper()
return signature
def init(self, config: AlibabaConfig):
self.config = config self.session = requests.Session()
def get_product_detail(
self, product_id: str, fields: str = None, language: str = "en", include_raw: bool = False
) -> Optional[Dict]:
""" 获取商品详情 Args: product_id: 商品ID fields: 指定返回字段,如 "subject,price_info,sku_list" language: 语言代码,默认英文 include_raw: 是否包含原始HTML描述 Returns: 商品详情数据字典 """ # 构建请求参数 params = # 添加可选字段 if fields: params['fields'] = fields if include_raw: params['include_raw'] = 'true' # 生成签名 params['sign'] = self._generate_signature(params) params['access_token'] = self.config.access_token try: response = self.session.get( f"{self.config.base_url}/aliexpress.open/api.getproductdetail", params=params, timeout=30 ) response.raise_for_status() result = response.json() # 检查API返回状态 if result.get('code') == 200: return result.get('model', {}).get('product_detail') else: print(f"API错误: ") return None except requests.exceptions.RequestException as e: print(f"请求异常: {str(e)}") return None
def _generate_signature(self, params: dict) -> str:
"""生成请求签名""" # 移除已存在的sign参数 params = # 按ASCII排序 sorted_params = sorted(params.items()) # URL编码并拼接 param_str = '&'.join([ f"{k}={urllib.parse.quote(str(v), safe='')}" for k, v in sorted_params ]) # 生成签名 sign_str = f"{self.config.app_secret}{param_str}{self.config.app_secret}" return hmac.new( self.config.app_secret.encode('utf-8'), sign_str.encode('utf-8'), hashlib.sha1 ).hexdigest().upper()
if name == “main”:
config = AlibabaConfig(
app_key=“your_app_key”,
app_secret=“your_app_secret”,
access_token=“your_access_token”
)
api = AlibabaProductAPI(config)
获取商品详情
product_id = “29” detail = api.get_product_detail(
product_id=product_id, fields="subject,price_info,sku_list,supplier_info,trade_info", include_raw=True
)
if detail:
print(f"商品标题: ") print(f"商品价格: ") print(f"SKU数量: ")
Detailed HTML description…
”,“product_price”: “25.00”,
“original_price”: “35.00”,
“discount”: “28%”,
“currency”: “USD”,
“moq”: 100,
“sku_list”: [
{
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容,请联系我们,一经查实,本站将立刻删除。
如需转载请保留出处:https://51itzy.com/kjqy/265422.html