通过python访问workdy RAAS报告时出错



我正试图通过python访问工作日报告。我可以使用userid和passwd通过浏览器访问它。但当我运行python时,我会得到以下错误。
import os
import platform
import ssl
import urllib.request
import urllib.parse
ssl_context = ssl.SSLContext(ssl.PROTOCOL_TLS)
ssl_context.verify_mode = ssl.CERT_REQUIRED
ssl_context.check_hostname = True
ssl_context.load_default_certs()
if platform.system().lower() == 'windows':
import certifi
print(os.path.relpath(certifi.where()))
ssl_context.load_verify_locations(
#cafile=os.path.relpath(certifi.where()),
cafile="C:\abc_Tools\TDX_INT166\Lib\site-packages\certifi\workday.pem",
capath=None,
cadata=None)
print(platform.system().lower())
url = 'https://wd5-impl-services1.workday.com/ccx/service/customreport2/xxx/ISU_INT167/CR_- 
_FIN_Report'
username = 'XXXXXXXXX'  # 10 digit ID
password = 'XXXXXXXXX'
values ={'username' : username, 'password':password}
data = urllib.parse.urlencode(values).encode("utf-8")
#cookies = cookielib.CookieJar()
https_handler = urllib.request.HTTPSHandler(context=ssl_context)
opener = urllib.request.build_opener(https_handler)
ret = opener.open(url, timeout=2)
print(ret)

我得到以下错误。

site-packagescertificacert.pem
windows
Traceback (most recent call last):
File "C:SLU_ToolsTDX_INT166LibCertificate_testing.py", line 38, in <module>
ret = opener.open(url, timeout=2)
File 
"C:UsersprasannakumaravelAppDataLocalProgramsPythonPython39liburllibrequest.py", 
line 523, in open
response = meth(req, response)
File 
"C:UsersprasannakumaravelAppDataLocalProgramsPythonPython39liburllibrequest.py", 
line 632, in http_response
response = self.parent.error(
File 
"C:UsersprasannakumaravelAppDataLocalProgramsPythonPython39liburllibrequest.py", 
line 561, in error
return self._call_chain(*args)
File 
"C:UsersprasannakumaravelAppDataLocalProgramsPythonPython39liburllibrequest.py", 
line 494, in _call_chain
result = func(*args)
File 
"C:UsersprasannakumaravelAppDataLocalProgramsPythonPython39liburllibrequest.py", 
line 641, in http_error_default
raise HTTPError(req.full_url, code, msg, hdrs, fp)
urllib.error.HTTPError: HTTP Error 401: Unauthorized

我也尝试了其他方法。但什么都没用。目前为止这可行吗?

以这种方式调用RAAS时,需要使用Basic Auth进行身份验证。用户名/密码需要进行Base64编码,结果将作为HTTP标头添加。请参阅此答案以获取示例。

您需要使用Basic Auth类型设置授权,并且需要按照Basic Auth方案对用户名/密码进行编码——首先将凭据转换为base64,然后再转换回字符串格式,并在授权字符串中使用它。

我在我们的网络应用程序中使用了类似于下面片段的代码:

import requests, base64
url = '<Workday_RaaS_API_Endpoint>'
username = '<your_RaaS_user_username>'
password = '<your_RaaS_user_password>' # replace with the password here
auth = 'Basic %s' % base64.b64encode(bytes('%s:%s' % (username, password), 'utf-8')).decode('utf-8')
headers = { 'Authorization': auth }
res = requests.get(url=url, headers=headers)
print(res.json())

最新更新