parkerhancock / patent_client

A collection of ORM-style clients to public patent data
Other
78 stars 29 forks source link

Problem with EPO credentials #170

Open deg opened 1 month ago

deg commented 1 month ago

I've moved past my earlier false alarm in #160 but now have a similar problem with the EPO API.

patent_client._sync.epo.ops.session.OpsAuthenticationError: Failed to authenticate with EPO OPS! Please check your credentials. See the setup instructions at https://patent-client.readthedocs.io/en/stable/getting_started.html

I do have the credentials set correctly, I think:

>>> os.environ["PATENT_CLIENT_EPO_API_KEY"]
'2pwQ_ELIDED_VBTl4'
>>> os.environ["PATENT_CLIENT_EPO_SECRET"]
'pJA__ELIDED__WHH'

I've also successfully called the EPO API directly with code like

    auth_url = "https://ops.epo.org/3.2/auth/accesstoken"
    response = requests.post(
        auth_url,
        auth=HTTPBasicAuth(client_id, client_secret),
        data={"grant_type": "client_credentials"},
    )

    if response.status_code == 200:
        token = response.json().get("access_token")
        return token
...
    base_url = (
        "https://ops.epo.org/3.2/rest-services/published-data/publication/epodoc/"
    )
    headers = {"Authorization": f"Bearer {access_token}", "Accept": "application/json"}
    response = requests.get(base_url + publication_number, headers=headers)

I assume this is my operator error again, but I don't see where.

Do I have the correct keys? developers.epo.org tells me that I have a "consumer key" and consumer secret key". Are these the same thing as the EPO OPS API keys that your API needs? Or do I need to get a different set?

wschlecht commented 1 month ago

Hi David,

I am having a similar issue, which is odd because I was able to successfully use my EPO OPS API keys with patent-client just a week or so ago, but today I am getting an "Inpadoc: Failed to authenticate with EPO OPS!" error.

One thing which appears incorrect in your code is that "PATENT_CLIENT_EPO_SECRET" should be "PATENT_CLIENT_EPO_API_SECRET"

I was (at least previously) able to successfully use my EPO OPS keys with patent-client using the below code: os.environ['PATENT_CLIENT_EPO_API_KEY'] = ##### os.environ['PATENT_CLIENT_EPO_API_SECRET'] = #####

So perhaps try changing the names of your environment variables and see if that works for you, but as I said I'm encountering a similar issue, despite being able to authenticate my EPO OPS keys on https://developers.epo.org/ (with their test API), so it's possible there is something else going on.

wschlecht commented 1 month ago

I was actually able to identify that the source of my particular error was coming from patent_client/_sync/epo/ops/session.py and in particular the build_refresh_request method of the OpsAuth class. I was able to fix my issue by reformatting the header of the request as follows:

def build_refresh_request(self): token = base64.b64encode(f"{self.key}:{self.secret}".encode()).decode('utf-8') headers = {"Authorization": f"Basic {token}"} return httpx.Request( "POST", self.auth_url, headers=headers, data={"grant_type": "client_credentials"} )

This is not my area of expertise, but unless I am missing something I don't believe the current patent-client library will be able to successfully authenticate EPO OPS credentials until this method is patched to have the correct header format.