【问题标题】:Python Request Token from REST API来自 REST API 的 Python 请求令牌
【发布时间】:2020-04-06 21:28:20
【问题描述】:

我正在努力处理这个文档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’

 }

 “`

如何将 with 变成 requests.post() ?

【问题讨论】:

    标签: python api request restful-authentication


    【解决方案1】:

    你必须建立字典并用请求发布它们:

    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))
    

    【讨论】:

      猜你喜欢
      • 2016-06-26
      • 2014-11-27
      • 1970-01-01
      • 2013-03-20
      • 1970-01-01
      • 2021-12-23
      • 2020-12-10
      • 1970-01-01
      相关资源
      最近更新 更多