我正在尝试使用 HTTPBasicAuth 从 API 返回 GET 请求。
我已经在 Postman 中测试了以下内容,并收到了正确的回复
URL:"https://someapi.data.io"
username:"username"
password:"password"
这会返回我期望的数据,一切都很好。
然而,当我在 python 中尝试这个时,我得到了一个 403 错误
,以及一个
""error_type":"ACCESS DENIED","message":"Please confirm api-key, api-secret, and permission is correct."
下面是我的代码:
import requests
from requests.auth import HTTPBasicAuth
URL = 'https://someapi.data.io'
authBasic=HTTPBasicAuth(username='username', password='password')
r = requests.get(URL, auth = authBasic)
print(r)
老实说,我不知道为什么这不起作用,因为使用 HTTPBasicAuth 在 Postman 中传递了相同的用户名和密码
最佳答案
您没有传达所有必需的参数。 postman 会自动为您执行此操作。
为了能够在 python 请求中使用,只需指定所有必需的参数。
headers = {
'Host': 'sub.example.com',
'User-Agent': 'Chrome v22.2 Linux Ubuntu',
'Accept': '*/*',
'Accept-Encoding': 'gzip, deflate, br',
'Connection': 'keep-alive',
'X-Requested-With': 'XMLHttpRequest'
}
url = 'https://sub.example.com'
response = requests.get(url, headers=headers)
关于python - 从 python get 请求返回 403 错误,但 auth 在 postman 中有效?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/71255965/