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
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
在.NET Core Web API中,你可以使用JWT(JSON Web Token)来进行身份验证和授权。要获取当前登录用户,你需要在每个请求中检查JWT令牌,并将其解码以获取用户信息。以下是一些步骤: 1. 在你的.NET Core Web API应用程序中添加JWT身份验证中间件。你可以使用现有的第三方库,如Microsoft.AspNetCore.Authentication.JwtBearer,或者自己编写中间件。 2. 在身份验证成功后,从请求获取JWT令牌。你可以从HTTP请求头中获取令牌,或者从请求正文中获取令牌。 3. 解码JWT令牌获取用户信息。JWT令牌包含有关用户的信息,例如用户名、角色和权限。你可以使用JWT库来解码令牌访问其内容。 以下是一些示例代码,演示如何在.NET Core Web API获取当前用户: ```csharp // 在Startup.cs中添加JWT身份验证中间件 public void Configure(IApplicationBuilder app, IWebHostEnvironment env) { app.UseAuthentication(); // ... } // 在Controller中获取当前用户 [Authorize] [ApiController] [Route("[controller]")] public class MyController : ControllerBase { // 获取当前用户的用户名 [HttpGet] public IActionResult Get() { var username = User.Identity.Name; return Ok($"Hello, {username}!"); } } ``` 在上面的代码中,我们使用了[Authorize]属性来确保只有经过身份验证的用户才能访问MyController。然后,在Get()方法中,我们使用User.Identity.Name属性来获取当前用户的用户名。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值