首页 > 解决方案 > 在 BeautifulSoup (Python) 中找不到亚马逊重定向的 url。从哪里获得重定向网址的任何想法?

问题描述

我正在尝试使用亚马逊广告 API 获取亚马逊广告数据。我必须打开一个特定链接:“ https://www.amazon.com/ap/oa?client_id=...&scope=cpc_advertising:campaign_management&response_type=code&redirect_uri=https://.../ ”,将我重定向到一个网址,我可以在其中提供我的登录数据。

为了获得那个重定向 url,我在 Python 中使用 BeautifulSoup。在 Web Inspector 中,我看到重定向 url 位于名称“location”下的响应标头中,但是当使用 BeautifulSoup 调用时,“location”丢失了。

from bs4 import BeautifulSoup

headers = {'user-agent': 'Thats the User Agent I get from the Web Inspector'}

p = requests.get(
    'Thats the link from which I wll be further redirected')

login_data = {
    'appAction': 'SIGNIN',
    'email': '...',
    'password': '...'
}

with requests.Session() as s:
    url = p.url
    r = s.get(url, headers=headers)
    soup = BeautifulSoup(r.content, 'html.parser')
    login_data['appActionToken'] = soup.find('input', attrs={'name': 'appActionToken'})['value']
    login_data['openid.return_to'] = soup.find('input', attrs={'name': 'openid.return_to'})['value']
    login_data['prevRID'] = soup.find('input', attrs={'name': 'prevRID'})['value']
    login_data['workflowState'] = soup.find('input', attrs={'name': 'workflowState'})['value']
    r = s.post(url, data = login_data, headers=headers)


    print(r.content)

我们可以从哪里获得重定向网址的任何想法?

标签: pythonpython-3.xbeautifulsoupamazon-advertising-api

解决方案


您自己就是您的应用程序的主人。您必须知道应用程序的重定向 url 并在 Login-with-Amazon 控制台中设置它。您可以在控制台中为每个应用程序设置多个重定向 URL。

就像是:

https://www.yourpage.com/login
http://localhost:8080/login

另请参阅: https ://advertising.amazon.com/API/docs/en-us/setting-up/generate-api-tokens


推荐阅读