Oauth 2.0 Python请求登录到Amazon以获取Amazon广告API的访问令牌

Oauth 2.0 Python请求登录到Amazon以获取Amazon广告API的访问令牌,oauth-2.0,amazon,url-redirection,amazon-advertising-api,Oauth 2.0,Amazon,Url Redirection,Amazon Advertising Api,我正在尝试使用python请求来接收Amazon广告API的访问令牌。程序概述如下:以下是我尝试的内容 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,

我正在尝试使用python请求来接收Amazon广告API的访问令牌。程序概述如下:以下是我尝试的内容

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)

我得到了一个错误\u description=User+not+authenticated&error=access\u denied错误,我在这里做错了什么?

我实际上也刚开始走这条路,这有点棘手,因为几乎所有可用的文档都是针对类似名称的Amazon产品广告API的

虽然我没有一个确切的答案,但我认为最直接的问题是您正在发布到原始URL,但Amazon在URL处处理登录请求:

如果你把url改成Amazon,你可能会收到验证码,因为他们的自动检测非常严格

你需要启用cookies来避免验证码,但是如果你身上有2FA,你也会被它击中。我会在几分钟内更新这个,当我找到cookies+2fa的最佳解决方案时

import requests
from bs4 import BeautifulSoup

client_email = EMAIL
client_pass = POSS
client_id = CLIENT_ID
return_url = RETURN_URL  # I'm not convinced this matters

# I just copied these values from my browser
headers = {
'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/74.0.3729.157 Safari/537.36',
'accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3',
'accept-encoding': 'gzip, deflate, br',
'accept-language': 'en-US,en;q=0.5',
'cache-control': 'max-age=0',
'referer': 'https://www.amazon.com/gp/sign-in.html',
'upgrade-insecure-requests': '1'
}
url = 'https://www.amazon.com/ap/oa?client_id=' + client_id + '&scope=cpc_advertising:campaign_management&' \
                                                          'response_type=code&redirect_uri=' + return_url

with requests.session() as s:
response = s.get(url, headers=headers)
cookies = dict(response.cookies)
soup = BeautifulSoup(response.text, 'html.parser')
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'] = client_email
data[u'password'] = client_pass

post_resp = s.post('https://www.amazon.com/ap/signin', data=data, headers=headers, cookies=cookies)

我能够想出如何使这个过程自动化。更多信息,请发邮件给我。

实际上,我也刚刚开始走这条路,这有点棘手,因为几乎所有可用的文档都是针对类似名称的Amazon产品广告API的

虽然我没有一个确切的答案,但我认为最直接的问题是您正在发布到原始URL,但Amazon在URL处处理登录请求:

如果你把url改成Amazon,你可能会收到验证码,因为他们的自动检测非常严格

你需要启用cookies来避免验证码,但是如果你身上有2FA,你也会被它击中。我会在几分钟内更新这个,当我找到cookies+2fa的最佳解决方案时

import requests
from bs4 import BeautifulSoup

client_email = EMAIL
client_pass = POSS
client_id = CLIENT_ID
return_url = RETURN_URL  # I'm not convinced this matters

# I just copied these values from my browser
headers = {
'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/74.0.3729.157 Safari/537.36',
'accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3',
'accept-encoding': 'gzip, deflate, br',
'accept-language': 'en-US,en;q=0.5',
'cache-control': 'max-age=0',
'referer': 'https://www.amazon.com/gp/sign-in.html',
'upgrade-insecure-requests': '1'
}
url = 'https://www.amazon.com/ap/oa?client_id=' + client_id + '&scope=cpc_advertising:campaign_management&' \
                                                          'response_type=code&redirect_uri=' + return_url

with requests.session() as s:
response = s.get(url, headers=headers)
cookies = dict(response.cookies)
soup = BeautifulSoup(response.text, 'html.parser')
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'] = client_email
data[u'password'] = client_pass

post_resp = s.post('https://www.amazon.com/ap/signin', data=data, headers=headers, cookies=cookies)

我能够想出如何使这个过程自动化。请向我发送消息以获取更多信息。

您不需要在Python脚本中使用用户名和密码进行身份验证! 您需要的是客户端ID、作用域和重定向URI以及三个请求:

  • 获取授权代码:

    GEThttps://www.amazon.com/ap/oa?client_id={{CLIENT_ID}}&scope={{scope}}&response_type=code&redirect_uri={{redirect_uri}}

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

  • 请求代币

    POSThttps://api.amazon.com/auth/o2/token

    带标题:

    Content-Type:application/x-www-form-urlencoded
    charset:UTF-8
    
    内容类型:应用程序/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}}
    
    grant_type:refresh_token
    refresh_token:{{REFRESH_TOKEN}}   <------ returned from step 2
    client_id:{{CLIENT_ID}}
    client_secret:{{CLIENT_SECRET}}
    
    身体数据:

    grant_type:authorization_code
    code:{{AUTH_CODE}}    <----- returned from step 1
    client_id:{{CLIENT_ID}}
    client_secret:{{CLIENT_SECRET}}
    redirect_uri:{{REDIRECT_URI}}
    
    grant_type:refresh_token
    refresh_token:{{REFRESH_TOKEN}}   <------ returned from step 2
    client_id:{{CLIENT_ID}}
    client_secret:{{CLIENT_SECRET}}
    

  • 您不需要在Python脚本中使用用户名和密码进行身份验证! 您需要的是客户端ID、作用域和重定向URI以及三个请求:

  • 获取授权代码:

    GEThttps://www.amazon.com/ap/oa?client_id={{CLIENT_ID}}&scope={{scope}}&response_type=code&redirect_uri={{redirect_uri}}

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

  • 请求代币

    POSThttps://api.amazon.com/auth/o2/token

    带标题:

    Content-Type:application/x-www-form-urlencoded
    charset:UTF-8
    
    内容类型:应用程序/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}}
    
    grant_type:refresh_token
    refresh_token:{{REFRESH_TOKEN}}   <------ returned from step 2
    client_id:{{CLIENT_ID}}
    client_secret:{{CLIENT_SECRET}}
    
    身体数据:

    grant_type:authorization_code
    code:{{AUTH_CODE}}    <----- returned from step 1
    client_id:{{CLIENT_ID}}
    client_secret:{{CLIENT_SECRET}}
    redirect_uri:{{REDIRECT_URI}}
    
    grant_type:refresh_token
    refresh_token:{{REFRESH_TOKEN}}   <------ returned from step 2
    client_id:{{CLIENT_ID}}
    client_secret:{{CLIENT_SECRET}}
    

  • 您好,tector,我不知道
    Amazon广告API作用域:{{profile\u id}}
    中的
    profile\u id
    是什么意思,您能告诉我如何获得profile\u id吗?您可以通过
    get获得您的profile id列表https://advertising-api.amazon.com/v2/profiles
    您将看到每个市场有一个配置文件id。请参阅:感谢您的反馈,我尝试了您的建议,但我总是收到类似以下错误的信息:
    {“code”:“UNAUTHORIZED”,“details”:“http401unauthorized”,“requestId”:“AAY9QFJ3NBEPJYBTK030”}
    https://advertising-api-test.amazon.com/v2/profiles
    https://advertising-api.amazon.com/v2/profiles
    我申请了API访问并注册了,但我没有收到来自亚马逊的accept电子邮件。对此我很困惑。好吧,你需要一封来自亚马逊的确认邮件才能拥有一个具有API访问权限的卖家帐户。请向亚马逊寻求支持。嗨,tector,我不知道
    Amazon广告API作用域中的
    profile\u id
    是什么意思:{{{profile\u id}}
    ,您能告诉我如何获得个人资料id吗?您可以通过
    get获得您的个人资料id列表https://advertising-api.amazon.com/v2/profiles
    您将看到每个市场有一个配置文件id。请参阅:感谢您的反馈,我尝试了您的建议,但我总是收到类似以下错误的信息:
    {“code”:“UNAUTHORIZED”,“details”:“http401unauthorized”,“requestId”:“AAY9QFJ3NBEPJYBTK030”}
    https://advertising-api-test.amazon.com/v2/profiles
    https://advertising-api.amazon.com/v2/profiles
    我申请了API访问并注册了,但我没有收到来自亚马逊的accept电子邮件。对此我很困惑。好吧,你需要一封来自亚马逊的确认邮件才能拥有一个具有API访问权限的卖家帐户。请向Amazon寻求支持。嗨,Guguma,你有自动化它的解决方案吗。我们每次都必须登录吗。提前谢谢。嗨,古古玛,你有自动化它的解决方案吗。我们每次都必须登录吗。提前谢谢。