从python连接到sparql端点时发生授权错误

2024-04-27 16:08:33 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在尝试从jupyter笔记本连接到专用SPARQL端点。我是python新手,不知道自己做错了什么。我已经提供了正确的登录凭据,但是当我尝试处理结果时,我遇到了一个无法解释的错误

我通过从浏览器访问端点并手动运行查询进行了检查。它工作得很好,我得到的回应也没有错误。 我不确定需要在request.py和Wrapper.py中更改什么来处理此错误

提前谢谢你的帮助。谢谢

sparql = SPARQLWrapper(" sparql address")
sparql.setHTTPAuth(DIGEST)
sparql.setCredentials("username", "pswd")
sparql.setQuery("""
  --query--
""")
sparql.setReturnFormat(CSV)
results = sparql.query().convert()

---------------------------------------------------------------------------
HTTPError                                 Traceback (most recent call last)
/tmp/ENTER/lib/python3.7/site-packages/SPARQLWrapper/Wrapper.py in _query(self)
    765             else:
--> 766                 response = urlopener(request)
    767             return response, self.returnFormat

/tmp/ENTER/lib/python3.7/urllib/request.py in urlopen(url, data, timeout, cafile, capath, cadefault, context)
    221         opener = _opener
--> 222     return opener.open(url, data, timeout)
    223 

/tmp/ENTER/lib/python3.7/urllib/request.py in open(self, fullurl, data, timeout)
    530             meth = getattr(processor, meth_name)
--> 531             response = meth(req, response)
    532 

/tmp/ENTER/lib/python3.7/urllib/request.py in http_response(self, request, response)
    640             response = self.parent.error(
--> 641                 'http', request, response, code, msg, hdrs)
    642 

/tmp/ENTER/lib/python3.7/urllib/request.py in error(self, proto, *args)
    568             args = (dict, 'default', 'http_error_default') + orig_args
--> 569             return self._call_chain(*args)
    570 

/tmp/ENTER/lib/python3.7/urllib/request.py in _call_chain(self, chain, kind, meth_name, *args)
    502             func = getattr(handler, meth_name)
--> 503             result = func(*args)
    504             if result is not None:

/tmp/ENTER/lib/python3.7/urllib/request.py in http_error_default(self, req, fp, code, msg, hdrs)
    648     def http_error_default(self, req, fp, code, msg, hdrs):
--> 649         raise HTTPError(req.full_url, code, msg, hdrs, fp)
    650 

HTTPError: HTTP Error 401: Unavailable

During handling of the above exception, another exception occurred:

Unauthorized                              Traceback (most recent call last)
<ipython-input-6-14dac11c9be4> in <module>
      1 sparql.setReturnFormat(CSV)
      2 
----> 3 results = sparql.query().convert()
      4 
      5 #for result in results["results"]["bindings"]:

/tmp/ENTER/lib/python3.7/site-packages/SPARQLWrapper/Wrapper.py in query(self)
    796             @rtype: L{QueryResult} instance
    797         """
--> 798         return QueryResult(self._query())
    799 
    800     def queryAndConvert(self):

/tmp/ENTER/lib/python3.7/site-packages/SPARQLWrapper/Wrapper.py in _query(self)
    772                 raise EndPointNotFound(e.read())
    773             elif e.code == 401:
--> 774                 raise Unauthorized(e.read())
    775             elif e.code == 500:
    776                 raise EndPointInternalError(e.read())

Unauthorized: Unauthorized: access is denied due to invalid credentials (unauthorized). Check the credentials. 

PS:我将身份验证更改为BASIC,并将login和password中的双引号替换为单引号,现在我得到了json响应。我不确定是解决方案的身份验证类型还是服务器过载,因为我没有得到响应


Tags: inpyselfhttpresponserequestlibargs