Python 从 OPS API 得到 access_token

Python get access_token from OPS API

我一直在尝试使用 python 中的请求包连接到 EPO OPS API - 但没有成功。 dev guide(第 34 页)指出第 1 步是将消费者密钥和消费者秘密转换为 Base64Encode(消费者 key:Consumer 秘密)。第 2 步是使用基本身份验证请求访问令牌,通过加密的 HTTPS 连接使用 base64Encoding 提供其消费者密钥和消费者机密,如下所示:

下面是我使用的代码,但我收到错误 400 状态。

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)

有人知道如何使 access_token 请求生效吗?

我想要脚本提取此 link 的 XML 内容:

"http://ops.epo.org/3.2/rest-services/published-data/search?q=automation"

谢谢!

我得到了这个的解决方案,我忘了添加 grant_type 参数。解决方案如下:

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)