Python从OPS API获取access_令牌

2024-04-26 00:42:45 发布

您现在位置:Python中文网/ 问答频道 /正文

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

enter image description here

下面是我使用过的代码,但我得到一个错误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请求工作吗?在

我希望脚本提取此链接的XML内容:

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

谢谢!在


Tags: keyorgimporttokenurl密钥步骤使用者
1条回答
网友
1楼 · 发布于 2024-04-26 00:42:45

我得到了这个问题的解决方案,我忘了添加grant_类型参数。解决方案如下:

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)

相关问题 更多 >

    热门问题