python连接服务器api获取token获取数据,Python请求登录到Amazon登录以获取Amazon Advertising API的访问令牌...

I am trying to use python requests to receive my access token for the Amazon Advertising API. The procedure is outlined here: https://advertising.amazon.com/API/docs/v2/guides/authorization Here is what I tried

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)

i get an error_description=User+not+authenticated&error=access_denied error, what am I doing wrong here?

解决方案

You DON'T NEED Username and Password in your Python Script to authenticate!

What you need is CLIENT_ID, SCOPE and REDIRECT_URI and three requests:

Get authorization code:

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

This will open the 'Login with Amazon' Consent Page, where you (or your customer) log into your Amazon Seller Central account and grant access to the Console APP with API access rights.

Request tokens

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

with headers:

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

with body data:

grant_type:authorization_code

code:{{AUTH_CODE}}

client_id:{{CLIENT_ID}}

client_secret:{{CLIENT_SECRET}}

redirect_uri:{{REDIRECT_URI}}

Get/Refresh access token (every time it is outdated):

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

with headers:

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

charset:UTF-8

with body data:

grant_type:refresh_token

refresh_token:{{REFRESH_TOKEN}}

client_id:{{CLIENT_ID}}

client_secret:{{CLIENT_SECRET}}

With the CLIENT_ID and (fresh) access token you can now request every service from the API. For excample listCampaigns:

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

Headers:

Content-Type:application/json

Amazon-Advertising-API-ClientId:{{CLIENT_ID}}

Amazon-Advertising-API-Scope:{{PROFILE_ID}}

Authorization:Bearer {{ACCESS_TOKEN}}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是使用 Python 登录 CSDN,清除 token,使用 cookies 发送 POST 请求编辑个人资料,获取返回结果到本地的示例代码: ```python import requests from bs4 import BeautifulSoup # 登录 CSDN login_url = 'https://passport.csdn.net/v1/register/pc/login/doLogin' headers_login = { 'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.36 Edge/16.16299', 'Referer': 'https://passport.csdn.net/login?code=public' } data_login = { 'loginType': '1', 'pwdOrVerifyCode': 'your_password', 'userIdentification': 'your_username', 'uaToken': '' } session = requests.session() response_login = session.post(login_url, headers=headers_login, data=data_login) # 清除 token soup = BeautifulSoup(response_login.text, 'html.parser') token = soup.find('input', attrs={'name': 'csrfmiddlewaretoken'})['value'] session.cookies.clear() # 使用 cookies 发送 POST 请求编辑个人资料 profile_url = 'https://i.csdn.net/settings/profile' headers_profile = { 'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.36 Edge/16.16299', 'Referer': 'https://i.csdn.net/settings/profile' } data_profile = { 'csrfmiddlewaretoken': token, 'nickname': 'new_nickname', 'email': '[email protected]', 'realname': 'new_realname', 'gender': '1', 'birthday': '1990-01-01', 'company': 'new_company', 'job_title': 'new_job_title', 'phone': 'new_phone', 'qq': 'new_qq', 'wechat': 'new_wechat', 'weibo': 'new_weibo', 'github': 'new_github', 'gitee': 'new_gitee', 'description': 'new_description', 'blog': 'new_blog', 'blog_rss': 'new_blog_rss', 'blog_title': 'new_blog_title', 'blog_tagline': 'new_blog_tagline', 'blog_category': 'new_blog_category', 'blog_keywords': 'new_blog_keywords' } response_profile = session.post(profile_url, headers=headers_profile, data=data_profile) # 获取返回结果到本地 with open('profile.html', 'w', encoding='utf-8') as f: f.write(response_profile.text) ``` 注意,代码中的 `your_username` 和 `your_password` 需要替换成你自己的 CSDN 账号和密码,`new_nickname`、`[email protected]` 等字段也需要根据实际情况修改。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值