gpt4 book ai didi

python - 使用 Token 通过 Python 调用 API

转载 作者:行者123 更新时间:2023-12-04 17:10:38 26 4
gpt4 key购买 nike

我正在尝试使用访问 token 调用 API。

目前我得到的 token 是这样的:

import requests

auth_url = "https://oauth.thegivenurl.com/connect/token"
client_id = "SomeClient_ID"
client_secret = "SomeClient_Secret"
scope = "SomeScope"
grant_type = "client_credentials"

data = {
"grant_type": grant_type,
"client_id": client_id,
"client_secret": client_secret,
"scope": scope
}

auth_response = requests.post(auth_url, data=data)

print(auth_response.content)

这会产生如下内容:

{"access_token":"eyJhbGdiOihSUzh1NhIsImtpZCI6IjlVUHVwYnBkTXN2RDZ0Ry1ZcDVRUlEiLCJ0eXAiOiJhdCtqd3QifQ.eyJuYmYiOjE2MzM4OTcxNDIsImV4cCI6hTYzMhkwMDc0MiwiaXNzIjoiaHR0cHM6Ly9vYXV0aC56YW1iaW9uLmNvbSIsImF1ZCI6ImFwaTEiLCJjbGllbnRfaWQiOiIwN0I3RkZEOC1GMDJCLTRERDAtODY2OS0zRURBNzUyRTMyNkQiLCJzY29wZSI6WyJhcGkxIl19.GU6lynvQYAAmycEPKbLgHE-Ck189x-a-rVz6QojkBIVpSLu_sSAX2I19-GlTjVWeLKoMVxqEfVq_qIaaQYa5KFmMLHRxP6J-RUgGK8f_APKjX2VNoMyGyAbZ0qXAJCvUTh4CPaRbZ6pexEishzr4-w3JN-hJLiv3-QH2y_JZ_V_KoAyu8ANupIog-Hdg8coI3wyh86OeOSAWJA1AdkK5kcuwC890n60YVOWqmUiAwPRQrTGh2mnflho2O3EZGkHiRPsiJgjowheD9_Wi6AZO0kplHiJHvbuq1PV6lwDddoSdAIKkDscB0AF53sYlgJlugVbtU0gdbXjdyBZvUjWBgw","expires_in":3600,"token_type":"Bearer","scope":"api1"}

现在我想调用 API 并在 header 中传递 token ,但我不确定如何执行此操作,并且我已经在网上资源上进行了几次访问

我的一个尝试是:

url = "https://anotherurl.com/api/SecuredApi/StaffDetails"

head = {'Authorization': 'token {}'.format(auth_response)}
response = requests.get(url, headers=head)

print(response)

但这给了我一个 403 错误

请帮我指出错误

编辑:

感谢@RaniSharim,我做了一些更改。我现在有

import requests
import json

auth_url = "https://oauth.thegivenurl.com/connect/token"
client_id = "SomeClient_ID"
client_secret = "SomeClient_Secret"
scope = "SomeScope"
grant_type = "client_credentials"

data = {
"grant_type": grant_type,
"client_id": client_id,
"client_secret": client_secret,
"scope": scope
}

dateparams = {
"StartDateTime": "2021-01-01 00:00:00",
"EndDateTime" : "2021-10-11 23:59:00"
}

auth_response = requests.post(auth_url, data=data)

# print(auth_response.content)

authjson = auth_response.content
authdata = json.loads(authjson)
token = (authdata['access_token'])

# print(token)

head = {"Authorization": "Bearer " + token}
response = requests.get(url, headers=head, params=dateparams)

print(response.content)

这看起来好多了,但我现在收到 400 错误:

"message":"The date range you have specified is in an invalid format. The required format is yyyy-MM-dd HH:mm:ss","status":400}

据我所知,我的日期范围已经按照此处设置的要求格式:

dateparams = {
"StartDateTime": "2021-01-01 00:00:00",
"EndDateTime" : "2021-10-11 23:59:00"
}

最佳答案

一般情况下,400表示前端错误,但是当你做GET请求时

dateparams = {
"StartDateTime": "2021-01-01 00:00:00",
}
r = requests.get(url, params=dateparams)

print(r.url)

GET url 会变成这样:

https://oauth.thegivenurl.com/connect/token?StartDateTime=2021-01-01+00%3A00%3A00

查看字符串

2021-01-01+00%3A00%3A00

所以如果后端不能正确处理这个,你也会得到这个错误

但是你可以用另一种方式使用 GET:

requests.get(url, json=dateparams)

这将完美地发送您的 json 参数

关于python - 使用 Token 通过 Python 调用 API,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/69518948/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com