为了账号安全,请及时绑定邮箱和手机立即绑定

如何使用带有授权类型“客户端凭据”的请求 OAuthlib?

如何使用带有授权类型“客户端凭据”的请求 OAuthlib?

繁星coding 2022-05-24 13:19:39
所以我尝试调用一个仅在文档中提供令牌 url 的 API。为此,我想使用 python requests 包中的 OAuthlib。当我查看他们的文档时,他们给出了这个例子:# Credentials you get from registering a new applicationclient_id = '<the id you get from github>'client_secret = '<the secret you get from github>'# OAuth endpoints given in the GitHub API documentationauthorization_base_url = 'https://github.com/login/oauth/authorize'token_url = 'https://github.com/login/oauth/access_token'from requests_oauthlib import OAuth2Sessiongithub = OAuth2Session(client_id)# Redirect user to GitHub for authorizationauthorization_url, state = github.authorization_url(authorization_base_url)print ('Please go here and authorize,', authorization_url)# Get the authorization verifier code from the callback urlredirect_response = input('Paste the full redirect URL here:')# Fetch the access tokengithub.fetch_token(token_url, client_secret=client_secret,        authorization_response=redirect_response)# Fetch a protected resource, i.e. user profiler = github.get('https://api.github.com/user')print (r.content)但在 API 文档中,该服务仅提供令牌 url。它给出了这个 Http BodyPOST示例:Method: POSTHost: https://login.bol.com/tokenContent-Type: application/x-www-form-urlencodedAccept: application/jsonBody: client_id=oRNWbHFXtAECmhnZmEndcjLIaSKbRMVE&client_secret= MaQHPOnmYkPZNgeRziPnQyyOJYytUbcFBVJBvbMKoDdpPqaZbaOiLUTWzPAkpPsZFZbJHrcoltdgpZolyNcgvvBaKcmkqFjucFzXhDONTsPAtHHyccQlLUZpkOuywMiOycDWcCySFsgpDiyGnCWCZJkNTtVdPxbSUTWVIFQiUxaPDYDXRQAVVTbSVZArAZkaLDLOoOvPzxSdhnkkJWzlQDkqsXNKfAIgAldrmyfROSyCGMCfvzdQdUQEaYZTPEoA&grant_type=client_credentials或者这个 HTTP 标头POST示例:Method: POSTHost: https://login.bol.com/token?grant_type=client_credentialsAccept: application/jsonAuthorization: Basic <credentials><credentials>的串联在哪里<client_id>:<client_secret>。如何通过此 API 使用请求 OAuthlib?因为API 文档没有说明任何授权基础 url。
查看完整描述

1 回答

?
RISEBY

TA贡献1856条经验 获得超5个赞

我认为你可以提供<client_id>:<client_secret>这样的:


from oauthlib.oauth2 import BackendApplicationClient

client = BackendApplicationClient(client_id=client_id)

oauth = OAuth2Session(client=client)

token = oauth.fetch_token(token_url='https://provider.com/oauth2/token', client_id=client_id,

        client_secret=client_secret)

看到这个


查看完整回答
反对 回复 2022-05-24
  • 1 回答
  • 0 关注
  • 105 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信