问题描述
我想在 python 中使用 boto3 连接到卖家合作伙伴 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})
如果我能提供更多帮助,请告诉我:)