问题
I secured my SPARQL endpoint via SQL Accounts according to VirtSPARQLProtectSQLDigestAuthentication.
Before this operation, I can get the data through the code:
from SPARQLWrapper import SPARQLWrapper, JSON, DIGEST
sparql = SPARQLWrapper("http://example.org/sparql")
sparql.setQuery("...")
sparql.setReturnFormat(JSON)
results = sparql.query().convert()
And after that, I use the DIGEST way to get the data,
from SPARQLWrapper import SPARQLWrapper, JSON, DIGEST
sparql = SPARQLWrapper("http://example.org/sparql")
sparql.setHTTPAuth(DIGEST)
sparql.setCredentials('login', 'password')
sparql.setQuery("...")
sparql.setReturnFormat(JSON)
results = sparql.query().convert()
, Error 401 occured:
Traceback (most recent call last): File "1.py", line 21, in results = sparql.query().convert() File "/usr/local/lib/python2.7/site-packages/SPARQLWrapper/Wrapper.py", line 601, in query return QueryResult(self._query()) File "/usr/local/lib/python2.7/site-packages/SPARQLWrapper/Wrapper.py", line 581, in _query raise e urllib2.HTTPError: HTTP Error 401: Unauthorized
Anything wrong with my operations? The username and password are both correct.
Appreciate that if anyone can help.
回答1:
Well, I found the answer just several minutes after I wrote the problem, which reminds me of RTFSC again.
line 574~581 in Wrapper.py :
elif self.http_auth == DIGEST:
realm = "SPARQL"
pwd_mgr = urllib2.HTTPPasswordMgr()
pwd_mgr.add_password(realm, uri, self.user, self.passwd)
opener = urllib2.build_opener()
opener.add_handler(urllib2.HTTPDigestAuthHandler(pwd_mgr))
urllib2.install_opener(opener)
Besides user
and password
, there is another varible,realm
.(default value is "SPARQL"
), but VirtSPARQLProtectSQLDigestAuthentication set the realm as "SPARQL Endpoint"
.
So the solution is just changing your virtuoso's realm to "SPARQL"
.
来源:https://stackoverflow.com/questions/44254935/sparqlwrapper-http-error-401-unauthorized