Mathias
Mathias

Reputation: 207

Python get info from API / Oauth Authentication

that is my first try with an API, said API being called OPS. I would like to get information using the API (OAuth 2) within my python code.

The ressource URL is : http://ops.epo.org/3.2/rest-services/register/{publication}/{EPODOC}/{EP2814089}/biblio

I also received :

Consumer Key: O220VlTQqAmodifiedsf0YeqgM6c

Consumer Secret Key: swWmodified3edjORU

The documentation states that:

OPS uses the OAuth framework for Authentication and Authorization. At this point in time, only the “Client Credentials” flow is supported using a Consumer key and Consumer secret.

The actual steps to follow are:

Step 1: Client converts Consumer key and Consumer secret to Base64Encode(Consumer key:Consumer secret). This should be done programmatically using the language you are developing the client application in. For the purposes of this example, a public website was used to perform this conversion. By entering the colon separated Client credentials, an encoded response is generated. This response is then be used for basic Authentication.

Step 2: Client requests an access token using Basic Authentication, supplying its Consumer key and Consumer secret with base64Encoding over encrypted HTTPS connection:

enter image description here

OPS authenticates the client credentials passed in the Authorization header using basic authentication method. If credentials are valid, OPS responds with a valid access token.

Step 3: Client accesses OPS resources with access token in authorization header (bearer tokens) over encrypted HTTPS connection

enter image description here

I tried a few samples of code with requests but, until now, nothing worked.

Upvotes: 3

Views: 3321

Answers (2)

Mathias
Mathias

Reputation: 207

When using the previous response I can obtain a token. (Thanks a lot for your answer)

So I tried :

myUrl = 'http://ops.epo.org/3.2/rest-services/register/publication/EPODOC/EP2814089/biblio'

header = {'PRIVATE-TOKEN': myToken}
response = requests.get(myUrl, headers=header)
print(response.text)

but I obtained a 403 error.

I finally got a specific library to do the job :

EPO OPS Library

But I still don't know how to do it on my own...

Upvotes: 0

MvdD
MvdD

Reputation: 23436

The client credentials flow is described in the OAuth2 RFC-6749. The client id and secret are base64 encoded in a Basic authentication scheme as described in RFC-7617

You should be able to get a token using Python code like:

import requests
import base64

url = 'https://ops.epo.org/3.2/auth/accesstoken'
data = {"grant_type": "client_credentials"}

creds = base64.b64encode("O220VlTQqAmodifiedsf0YeqgM6c:swWmodified3edjORU".encode())
headers = {'Authorization': 'Basic ' + creds.decode('UTF-8'), 'Content-Type': 'application/x-www-form-urlencoded'}

response = requests.post(url, headers=headers, data=data)

access_token = response.json()["access_token"]

Upvotes: 2

Related Questions