0

I'm a registered user with Dydra and trying to update a repository programmatically in it. Even though I have logged in, I get a 401 error when trying to update a repository. Following is my code:

    username = 'myUsername'
    password = 'myPassword'
    URL = 'http://dydra.com/login'

   with requests.Session() as s:
    resp = s.get(URL)
    soup = BeautifulSoup(resp.text)
    csrfToken = soup.find('meta',{'name':'csrf-token'}).get('content')
    # print csrf_token
    payload = {
    'account[login]':username,
    'account[password]':password,
    'csrfmiddlewaretoken':csrfToken,
    'next':'/'
    }
    print payload

    p = s.post(URL,data=payload, headers=dict(Referer=URL))
    print p.text

    r = s.get('http://dydra.com/myUsername/repoName/sparql')
    print r.text

    for stmt in dataGraph:
        dydraSparqlEndpoint = 'http://dydra.com/myUsername/repoName/sparql'
        queryStringUpload = 'INSERT DATA {%s %s %s}' % stmt
        sparql = SPARQLWrapper(dydraSparqlEndpoint)
        sparql.setQuery(queryStringUpload)
        sparql.method = 'POST'
        sparql.query()

The code before the for statement works as expected. Why then is the insert statement causing an authentication error? What do I need to change in the code to successfully log in?

kurious
  • 1,024
  • 10
  • 29
  • I don't know enough about the API that you're using, but I don't see where you connect your user credentials to your sparql object. I see that you do a POST with the credentials earlier, but shouldn't they be included with the sparql query, too? – Joshua Taylor Dec 02 '15 at 23:03
  • I was under the impression that executing the SPARQL query inside the Session will take care of it. That clearly doesn't work. I'm trying to figure out how to include the credentials with the query. – kurious Dec 02 '15 at 23:13
  • Joshua is right, I'm not familiar with Dydra either, but in most of the graph databases you need to send you authentication with the post request. Are you only trying to post with the insert query? This is how Stardog functions: `resp = requests.post(transaction_endpoint, headers=HEADERS_RDF, data=open_file, auth=SPARQL_AUTH, palsrams={'graph-uri': graph})` – Artemis Dec 03 '15 at 09:49

1 Answers1

0

Based on the advice from Joshua and Artemis, I've been able to figure out the way to log in:

       key = my_dydra_key       
       dydraSparqlEndpoint = 'http://dydra.com/username/rep_name/sparql'
       for stmt in dataGraph:
            queryStringUpload = 'INSERT DATA {%s %s %s}' % stmt
            sparql = SPARQLWrapper(dydraSparqlEndpoint)
            sparql.setCredentials(key,key)
            sparql.setQuery(queryStringUpload)
            sparql.method = 'POST'
            sparql.query()

The important thing here is sparql.setCredentials(key,key). The key is the API key that Dydra gives you to log in programmatically. It's repeated because sparql.setCredentials needs username and password as parameters.

kurious
  • 1,024
  • 10
  • 29