0

我正在尝试使用 python 请求来接收我的亚马逊广告 API 访问令牌。此处概述了该过程:https ://advertising.amazon.com/API/docs/v2/guides/authorization这是我尝试过的

CLIENT_ID = MyClientID
CLIENT_SECRET = MySecret
RETURN_URL = 'https://myreturn.com/my.php'

headers = {
           'User-Agent': 'Mozilla/5.0 (Windows NT 6.3; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/44.0.2403.61 Safari/537.36',
          }

with requests.Session() as s:
    s.headers = headers
    r = s.get('https://www.amazon.com/ap/oa?client_id={}&scope=cpc_advertising:campaign_management&error=access_denied&response_type=code&redirect_uri={}'.format(CLIENT_ID,RETURN_URL),headers=headers)
    soup = BeautifulSoup(html)
    data = {}
    form = soup.find('form', {'name': 'signIn'})
    for field in form.find_all('input'):
        try:
            data[field['name']] = field['value']
        except:
            pass
    data[u'email'] = MY_EMAIL
    data[u'password'] = MY_PASS
    b = s.post('https://www.amazon.com/ap/oa?client_id={}&scope=cpc_advertising:campaign_management&response_type=code&redirect_uri={}',data=data,allow_redirects=True,headers=headers)

我收到 error_description=User+not+authenticated&error=access_denied 错误,我在这里做错了什么?

4

1 回答 1

3

您不需要在 Python 脚本中使用用户名和密码来进行身份验证!您需要的是CLIENT_ID、SCOPEREDIRECT_URI以及三个请求:

  1. 获取授权码:

    GET https://www.amazon.com/ap/oa?client_id={{CLIENT_ID}}&scope={{SCOPE}}&response_type=code&redirect_uri={{REDIRECT_URI}}

这将打开“使用亚马逊登录”同意页面,您(或您的客户)在此登录您的亚马逊卖家中心账户并授予对具有 API 访问权限的控制台应用程序的访问权限。

  1. 请求令牌

    POST https://api.amazon.com/auth/o2/token

    带标题:

    Content-Type:application/x-www-form-urlencoded

    带有身体数据:

    grant_type:authorization_code
    code:{{AUTH_CODE}}    <----- returned from step 1
    client_id:{{CLIENT_ID}}
    client_secret:{{CLIENT_SECRET}}
    redirect_uri:{{REDIRECT_URI}}
    
  2. 获取/刷新访问令牌(每次过期):

    POST https://api.amazon.com/auth/o2/token

    带标题:

    Content-Type:application/x-www-form-urlencoded
    charset:UTF-8
    

    带有身体数据:

    grant_type:refresh_token
    refresh_token:{{REFRESH_TOKEN}}   <------ returned from step 2
    client_id:{{CLIENT_ID}}
    client_secret:{{CLIENT_SECRET}}
    

  1. 使用 CLIENT_ID 和(新的)访问令牌,您现在可以从 API 请求每个服务。例如 listCampaigns:

    GET https://advertising-api.amazon.com/v2/sp/campaigns

    标题:

    Content-Type:application/json
    Amazon-Advertising-API-ClientId:{{CLIENT_ID}}
    Amazon-Advertising-API-Scope:{{PROFILE_ID}}
    Authorization:Bearer {{ACCESS_TOKEN}}   <----- returned from step 3
    
于 2019-07-01T14:34:09.787 回答