【问题标题】:Post Request to retrieve bearer token in Python发布请求以在 Python 中检索不记名令牌
【发布时间】:2023-03-16 07:57:02
【问题描述】:

我在执行发布请求以在 Python 中获取不记名令牌时遇到问题。

这是我当前使用硬编码不记名令牌的 Python 代码。

url = 'https://someURL'
headers = {'Authorization' : 'Bearer <MyToken>'} # I'm hard coding it here from a postman call I'm doing

r = requests.get(url, headers=headers)

#This part prints entire response content in a text like format [{'x':'' ,'y':'', ...etc},{'x':'' ,'y':'', ...etc},...etc]

jsonResponse = r.json()
print("Entire JSON response")
print(jsonResponse)

print("Print each key-value pair from JSON response")
for d in jsonResponse:
    for key, value in d.items():
        if(key == 'groupId'):
            print(key, ":", value)

我以前可以像这样在 JavaScript 中进行发布请求:

var options = {
    method: 'POST',
    url: 'https://myurl',
    qs:
    {
      grant_type: 'client_credentials',
      scope: 'api',
      client_id: 'xyz',
      client_secret: '123456abc'
    },
    headers:
    {
      'cache-control': 'no-cache',
    }
  };

如何在 Python 中获得不记名令牌发布请求?

【问题讨论】:

  • 您是在问如何在字符串中插入变量?或者你的 Python 代码有什么问题?
  • 我不明白 JS 代码与 Python 代码的关系。它根本没有'Authorization' 部分。

标签: python httprequest bearer-token


【解决方案1】:

您可以像这样创建一个简单的发布请求,然后使用您已经在做的response.json() 检索 json :)

data = {
  grant_type: 'client_credentials',
  scope: 'api',
  client_id: 'xyz',
  client_secret: '123456abc'
}

response = requests.post(url, data=data)
jsonResponse = response.json

【讨论】:

    猜你喜欢
    • 2020-02-21
    • 2020-03-19
    • 2019-02-20
    • 1970-01-01
    • 2021-05-18
    • 1970-01-01
    • 1970-01-01
    • 2017-03-20
    • 2020-12-11
    相关资源
    最近更新 更多