问题描述
我正在尝试使用python脚本从Amazon广告中获取报告,但找不到找到授权码的方法。所有的Amazon文档都是针对网站的,没有针对自动脚本的。
任何想法我该如何实施?
谢谢。
解决方法
您需要什么:
- 在
developers.amazon.com
上的注册申请 - 此应用程序的{li>
client_id
和client_secret
- 同意访问Amazon Advertising Account(通过“使用Amazon登录”完成)
通过同意流程,您将获得refresh token
。
使用client_id
,client_secret
和refresh token
,您可以构建一个Python脚本来接收access token
-使用access token
,您可以访问Amazon Advertising API。
资源:
https://advertising.amazon.com/API/docs/en-us/setting-up/account-setup
https://advertising.amazon.com/API/docs/en-us/get-started/overview
编辑: 我建议使用Postman或SoapUI之类的工具来轻松测试auth和api请求...
,我遇到了同样的问题,并使用 tector 的评论构建了一个简单的函数,该函数使用刷新令牌(永不过期)生成授权码,有效期为 60 分钟:
import requests
import json
host = 'https://api.amazon.com/auth/o2/token'
body = {
"grant_type": "refresh_token",'client_id': YOUR CLIENT ID,'refresh_token': YOUR REFRESH TOKEN,'client_secret': YOUR CLIENT SECRET
}
headers = {"Content-Type": "application/x-www-form-urlencoded;charset=UTF-8"}
def authenticator():
r = requests.post(host,body,headers=headers)
r.raise_for_status()
r = r.json()
with open('FILENAME.json','w') as f:
json.dump(r,f)