【发布时间】: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