我想使用 boto3 连接到 python 中的卖家合作伙伴 api。
获取会话客户端临时凭证的步骤
assumeRole
。但 sp-api
不在要使用 boto3 处理的 aws 服务列表中。是否有 sp-api
与 python 一起使用的参考,或者相当于 s3 = boto3.client('s3')
的 sp-api
?
我也有像你一样的问题和疑问,我成功连接,也许这可以帮助:
import boto3
# ======== GET AUTH ========
# Credentials for user created following the docs
amw_client = boto3.client(
'sts',
aws_access_key_id=self.access_key,
aws_secret_access_key=self.secret_key,
region_name=self.region
)
# ROLE created following the docs
# STS assume policy must be included in the role
res = amw_client.assume_role(
RoleArn='arn:aws:iam::xxxx:role/xxxx',
RoleSessionName='SellingPartnerAPI'
)
Credentials = res["Credentials"]
AccessKeyId = Credentials["AccessKeyId"]
SecretAccessKey = Credentials["SecretAccessKey"]
SessionToken = Credentials["SessionToken"]
from requests_auth_aws_sigv4 import AWSSigV4
aws_auth = AWSSigV4('execute-api',
aws_access_key_id=AccessKeyId,
aws_secret_access_key=SecretAccessKey,
aws_session_token=SessionToken,
region=self.region
)
import requests
# ======== GET ACCESS TOKEN ======
body = \
{
'grant_type': 'refresh_token',
'client_id': amazon_app_client_id,
'refresh_token': amazon_app_refresh_token,
'client_secret': amazon_app_client_secret
}
h = {'Content-Type': 'application/json'}
access_token_response = \
requests.post('https://api.amazon.com/auth/o2/token', json=body, headers=h)
access_token = self.access_token_response.json().get('access_token')
# ======== CONSUME API ========
resp = requests.get(
request_url, auth=aws_auth, headers={'x-amz-access-token': access_token})
如果我可以提供进一步帮助,请告诉我:)
在 2023 年,如果您的应用程序根据 注册概述 正确设置,您可以在没有 AWS 身份验证的情况下进行调用(从本线程中 @ky_aaaa 的答案重构的代码示例):
def get_access_token():
body = \
{
'grant_type': 'refresh_token',
'client_id': client_id,
'refresh_token': refresh_token,
'client_secret': client_secret
}
h = {'Content-Type': 'application/json'}
access_token_response = requests.post('https://api.amazon.com/auth/o2/token', json=body, headers=h)
access_token = access_token_response.json().get('access_token')
return access_token
report_status_url = 'https://sellingpartnerapi-na.amazon.com/reports/2021-06-30/reports/{}'
headers={'x-amz-access-token': get_access_token()}
resp = requests.get(report_status_url.format(reportId), headers=headers)
从哪里获取reportId值