【问题标题】:Use a client credentials flow with Python Requests将客户端凭据流与 Python 请求一起使用
【发布时间】:2021-09-17 11:41:38
【问题描述】:

对于一个项目,我尝试使用请求模块在 Python 中获取访问令牌。

它适用于 GET 方法:

import requests, json

# Oauth API - GET call - Winnie project
access_token = requests.get("https://api.ebanking.com/oauth/access_token?grant_type=client_credentials&client_id=27c5b286-62a6-45c7-beda-abbaea6eecf2&client_secret=6731de76-14a6-49ae-97bc-6eba6914391e").json()["access_token"]

但是,不能使用 POST 方法:

import requests, json

data = {'grant_type':'client_credentials', 
        'client_id':'27c5b286-62a6-45c7-beda-abbaea6eecf2', 
        'client_secret':'6731de76-14a6-49ae-97bc-6eba6914391e'} 

# Oauth API - POST call - Winnie project
access_token = requests.post(url = "https://api.ebanking.dev/oauth", data = data).json()["access_token"]

有什么想法吗?

【问题讨论】:

标签: python


【解决方案1】:

我遇到了同样的问题,但是当您使用 client_credential 身份验证时,您必须对授权进行编码并将标题和正文按顺序排列。

import base64, requests, sys


client_id = "client_id"
client_secret = "client_secret"

# Encode the client ID and client secret
authorization = base64.b64encode(bytes(client_id + ":" + client_secret, "ISO-8859-1")).decode("ascii")


headers = {
    "Authorization": f"Basic {authorization}",
    "Content-Type": "application/x-www-form-urlencoded"
}
body = {
    "grant_type": "client_credentials"
}

response = requests.post("https://url.com", data=body, headers=headers)

print(response.text)

【讨论】:

    猜你喜欢
    • 2020-02-23
    • 2021-12-22
    • 2022-11-09
    • 2021-01-21
    • 2015-11-14
    • 1970-01-01
    • 2015-06-16
    • 2020-10-10
    • 1970-01-01
    相关资源
    最近更新 更多