亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

如何使用帶有授權類型“客戶端憑據”的請求 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 關注
  • 151 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號