I've been trying to connect to the EPO OPS API using the requests package in python - without success. The dev guide (page 34) states that Step 1 is to convert Consumer key and Consumer secret to Base64Encode(Consumer key:Consumer secret). Step 2 is to request an access token using Basic Authentication, supplying its Consumer key and Consumer secret with base64Encoding over encrypted HTTPS connection as follows:
Below is the code I've used but I get an error 400 status.
import requests
import base64
url_token = "https://ops.epo.org/3.2/auth/accesstoken"
key = "Basic %s" %base64.b64encode("myconsumerkey:mysecretkey")
headers = ({"Authorization": key, 'Content-Type': 'application/x-www-form-urlencoded'})
resp = requests.post(url_token, headers=headers)
print(resp.status_code)
Anyone knows how to make the access_token request work?
I'd like the script to extract the XML content of this link:
"http://ops.epo.org/3.2/rest-services/published-data/search?q=automation"
Thanks!
I got the solution for this one, I forgot to add the grant_type parameter. Solution below:
import requests
import base64
import json
token_url='https://ops.epo.org/3.2/auth/accesstoken'
key = 'Basic %s' % base64.b64encode(b'myconsumerkeyhere:mysecretkeyhere').decode('ascii')
data = {'grant_type': 'client_credentials'}
headers = {'Authorization': key, 'Content-Type': 'application/x-www-form-urlencoded'}
r = requests.post(token_url, data=data, headers=headers)
rs= r.content.decode()
response = json.loads(rs)
token = response['access_token']
print(token)