我正在努力从 Python 调用 Rest-API。我已经使用 Postman 测试了所有 Rest-API,并且工作正常。但是,在使用 Python 脚本执行这些脚本时,我遇到了认证错误。
我正在从 Windows cmd 执行 python 脚本。
下面是代码:
import requests
import certifi
from urllib.request import urlopen
import ssl
requestCert = 'https://someurl.com:4443/api/11/projects/'
urlopen(requestCert, context=ssl.create_default_context(cafile=certifi.where()))
headers = {
"Authorization": "fdsfsfdewrwerwer",
"X-Auth-Token": "YxzcfhnkniGVGljghjmwCIGLfhfsfdzxc4o5sSADtaT2"
}
#response = requests.get('https://someurl.com:4443/api/11/projects', headers=headers)
#response = requests.get('https://someurl.com:4443/api/11/projects', headers=headers, verify=certifi.where())
response = requests.get('https://someurl.com:4443/api/11/projects', headers=headers, verify='server.pem')
print(response)
Run Code Online (Sandbox Code Playgroud)
进一步调试。。。即使简单import requests print('Hello, world!')并pip install requests给出相同的错误。我想问题不在代码中,而是与模块导入功能有关。
我收到的错误如下。
Traceback (most recent call last):
File "C:\Users\eddie\Desktop\PyPoc\Py_Job\importJob.py", line 1, in <module>
import requests
File "C:\Users\eddie\AppData\Local\Programs\Python\Python311\Lib\site-packages\requests\__init__.py", …Run Code Online (Sandbox Code Playgroud)