REST API的Python请求令牌

我在苦苦挣扎here

  • 将编码的base64凭据发送到身份验证服务器。
  • 获取包含用于身份验证的UUID的响应。
  • 使用UUID验证REST请求。

以该请求为例:

**Header**

“`json

{

‘Content-Type’: ‘application/x-www-form-urlencoded’,‘authorization’: ‘Basic <base64 encoded username:password>’

 }

 “`

 **Body**

 “`json

 {

 ‘grant_type’: ‘client_credentials’

 }

 “`

我如何变成一个request.post()?

jhk911 回答:REST API的Python请求令牌

您必须构建词典并向其发布请求:

import requests
import base64
import json

username = "user"
password = "password"
url = 'https://myurl.com'

headers = {}
headers['Content-Type'] = 'application/x-www-form-urlencoded'
headers['authorization'] = 'Basic ' + base64.b64encode(bytes(username + ':' + password,'utf-8')).decode('utf-8')

body = {}
body['grant_type'] = 'client_credentials'

r = requests.post(url,data=json.dumps(body),headers=json.dumps(headers))
本文链接:https://www.f2er.com/2927232.html

大家都在问