Post 请求在 Python 中检索不记名令牌

Post Request to retrieve bearer token in Python

我在执行 post 请求调用以获取 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 中执行 post 请求:

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 中工作的不记名令牌 post 请求?

您可以像这样创建一个简单的 post 请求,然后使用您已经在做的 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