簡體   English   中英

Python 從 OPS API 獲取 access_token

[英]Python get access_token from OPS API

我一直在嘗試使用 python 中的請求包連接到 EPO OPS API - 沒有成功。 開發指南(第 34 頁)指出,第 1 步是將消費者密鑰和消費者秘密轉換為 Base64Encode(消費者密鑰:消費者秘密)。 第 2 步是使用基本身份驗證請求訪問令牌,通過加密的 HTTPS 連接提供其消費者密鑰和消費者機密,如下所示:

在此處輸入圖片說明

以下是我使用過的代碼,但出現錯誤 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

謝謝!

我得到了這個解決方案,我忘了添加 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)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM