Authentication via API to SPARQL ENDPOINT

Hi @hwilliams,

I was reviewing the Openlink Virtuoso documentation on SPARQL authentication via API for a specific user. Is there documentation on how to perform a query using this method?

Thanks.

Regards.

What is the URL to the documentation on the SPARQL authentication via API you are referring ?

@hwilliams I am looking at: 16.2.3.SPARQL Web Services & APIs

The Examples section from the document link show how to interact with the the SPARQL Endpoint restful API via curl. And if you specifically want to authenticate against and endpoint then the --user curl param can be used to provide the username and password as below:

curl --digest --user {username}:{password} "http://{hostname}:{port-number}/sparql-auth" --data-urlencode "query={SPARQL-query}"

Hi @hwilliams,

I found also an authentication via oauth but I can’t find where are generated this two parameters:
1. oauth_consumer_key
2. oauth_token
Ex:

  curl "http://demo.openlinksw.com/oauth/sparql.vsp?debug=on&default-graph-uri=&format=text%2Fhtml&oauth_consumer_key=27f105a327f5f23163e0636f789018dacdd70bb5&oauth_nonce=a14d43339fcb2638&oauth_signature_method=HMAC-SHA1&oauth_timestamp=1242106643&oauth_token=42e2af4d9264ef42521c1010aff99f60a8ee95a2&oauth_version=1.0&query=select%20distinct%20%3FURI%20%3FObjectType%20where%20%7B%3FURI%20a%20%3FObjectType%7D%20limit%2050&oauth_signature=Cw9yJ2saU1vgHuFxWcughai5cZY%3D"

If you have VAL (Virtuoso Authentication Layer) installed, then the OpenID Connect (OIDC) and OAuth Protocol Virtualization, using Virtuoso details how to generate OAuth keys/tokens for use.

If VAL (Virtuoso Authentication Layer) is not installed then the Securing your SPARQL Endpoint via OAuth details how to generate the OAuth keys/tokens for use.

Hi @hwilliams,

I’m encountering an error when attempting to run the non-VAL procedure:

Error HTTP/1.1 404 File not found The requested URL was not found URI = ‘/oauth/’

Is it possible that I’m missing some package?

What are you testing against the Virtuoso open source or commercial build ? As the none VAL method only works with open source and not commercial as all OAuth functionality in the commercial builds are now in the VAL module which must be in place for OAuth to work.

Hi @hwilliams,

I was using the open version for this task …Also for the non commercial Virtuoso you must install Framework package.

In the commercial Version I have created via GUI this:

So, how can I invoke the endpoint SPARQL via CURL using token authentication ?

@hwilliams I dont know where to find parameters of the curl ex:

Example with curl and SPARQL-OAuth endpoint
  $ curl "http://demo.openlinksw.com/oauth/sparql.vsp?debug=on&default-graph-uri=&format=text%2Fhtml&oauth_consumer_key=27f105a327f5f23163e0636f78901
  8dacdd70bb5&oauth_nonce=a14d43339fcb2638&oauth_signature_method=HMAC-SHA1&oauth_timestamp=1242106643&oauth_token=42e2af4d9264ef42521c1010aff99f60a8
  ee95a2&oauth_version=1.0&query=select%20distinct%20%3FURI%20%3FObjectType%20where%20%7B%3FURI%20a%20%3FObjectType%7D%20limit%2050&oauth_signature=C
  w9yJ2saU1vgHuFxWcughai5cZY%3D"

Also this endpoint is not present: oauth/sparql.vsp
Ref.
https://docs.openlinksw.com/virtuoso/rdfsparqlprotocolendpoint/#rdfsparqlendpointexamples

Also where is set the relation between SPARQL USER → TOKEN
This scrip for Example:

import requests
from requests.packages.urllib3.exceptions import InsecureRequestWarning

# Suppress only the InsecureRequestWarning from urllib3 needed for this example
requests.packages.urllib3.disable_warnings(InsecureRequestWarning)

# Step 1: Acquire an access token
token_url = "https://localhost/val/api/thirdparty_callback"
token_params = {
    "grant_type": "client_credentials",
    "client_id": "b45a32b21192d9389a7e827bb1f7286eae1e59a4",
    "client_secret": "673141f3bcb70adee1b34fc3d5e19da2",
    "token_endpoint_auth_method": "client_secret_jwt"
}

# Ignore SSL verification (Not recommended for production)
token_response = requests.post(token_url, data=token_params, verify=False)

# Uncomment the following line if providing the CA certificates
# token_response = requests.post(token_url, data=token_params, verify='/path/to/ca_certificates.pem')

# Uncomment the following line to ensure correct hostname
# token_response = requests.post(token_url, data=token_params, verify=True, headers={'Host': 'yourhostname'})

token_data = token_response.json()

if 'access_token' not in token_data:
    print("Error getting access token")
    print(token_data)
    exit()

access_token = token_data['access_token']

# Step 2: Use the access token to make the SPARQL query
sparql_endpoint = "https://localhost/sparql"
sparql_query = "SELECT * WHERE {?s ?p ?o} LIMIT 10"  # Your SPARQL query goes here

headers = {
    "Authorization": f"Bearer {access_token}",
    "Content-Type": "application/sparql-query"
}

# Ignore SSL verification (Not recommended for production)
sparql_response = requests.post(sparql_endpoint, data=sparql_query, headers=headers, verify=False)

# Uncomment the following line if providing the CA certificates
# sparql_response = requests.post(sparql_endpoint, data=sparql_query, headers=headers, verify='/path/to/ca_certificates.pem')

# Uncomment the following line to ensure correct hostname
# sparql_response = requests.post(sparql_endpoint, data=sparql_query, headers=headers, verify=True, headers={'Host': 'yourhostname'})

if sparql_response.status_code == 200:
    print("SPARQL Query Results:")
    print(sparql_response.text)
else:
    print("Error executing SPARQL query")
    print(sparql_response.text)

Is getting this error:

Error getting access token
    {'status': 'error', 'httpcode': '500', 'code': 'other_error', 'message': 'SP029: Not enough input parameters in the request'}

Thanks.

What is the version of the VAL VAD you have installed ? As in the OAuth Client Application Dialog you provide below the Application Key and Secret options I do not see an API Key option to enable the generate of displaying a bearer token for use in the OAuth connection.

I have this option with VAL 2.2.4_git109/ 2024-01-15 installed on my machine and can make a curl OAuth connection with a command of the form curl -H "Authorization: Bearer XXX" --request POST 'https://localhost/sparql/?' --data-urlencode 'query=INSERT INTO GRAPH <http://example.org> {<4> <5> <6>}' where XXX is the API Key generated for the OAuth application ie

$ curl -H "Authorization: Bearer XXX" --request POST 'https://localhost/sparql/?' --data-urlencode 'query=INSERT INTO GRAPH  <http://example.org> {<4> <5> <6>}'
<sparql xmlns="http://www.w3.org/2005/sparql-results#" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.w3.org/2001/sw/DataAccess/rf1/result2.xsd">
 <head>
  <variable name="callret-0"/>
 </head>
 <results distinct="false" ordered="true">
  <result>
   <binding name="callret-0"><literal>Insert into &lt;http://example.org&gt;, 1 (or less) triples -- done</literal></binding>
  </result>
 </results>
</sparql>
$

Hi @hwilliams,

I have this version: 2.2.4_git78/ 2023-08-31 is possible to have your version, so I can make a test ?

Thanks.
Regards.

Attached is the 2.2.4_git109/ 2024-01-15 VAL VAD I am using …

val_dav.vad.zip (829.8 KB)

1 Like

Hi @hwilliams,

thanks is working fine :slight_smile: