[英]Python get access_token from OPS API
I've been trying to connect to the EPO OPS API using the requests package in python - without success.我一直在尝试使用 python 中的请求包连接到 EPO OPS API - 没有成功。 The dev guide (page 34) states that Step 1 is to convert Consumer key and Consumer secret to Base64Encode(Consumer key:Consumer secret).
开发指南(第 34 页)指出,第 1 步是将消费者密钥和消费者秘密转换为 Base64Encode(消费者密钥:消费者秘密)。 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:
第 2 步是使用基本身份验证请求访问令牌,通过加密的 HTTPS 连接提供其消费者密钥和消费者机密,如下所示:
Below is the code I've used but I get an error 400 status.以下是我使用过的代码,但出现错误 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)
Anyone knows how to make the access_token request work?任何人都知道如何使 access_token 请求工作?
I'd like the script to extract the XML content of this link:我希望脚本提取此链接的 XML 内容:
" http://ops.epo.org/3.2/rest-services/published-data/search?q=automation " “ 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.我得到了这个解决方案,我忘了添加 grant_type 参数。 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)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.