【发布时间】:2020-11-27 02:34:49
【问题描述】:
我是 python 新手,我正在尝试编写一个简单的程序,将 DNA 序列发送到 API 并返回一些简单的参数。据我了解,这使用 Oauth2.0 身份验证,因此我需要使用我的 clientID 和客户端密码请求令牌,然后通过发布请求将此令牌与数据一起传递以接收所需的输出。 API 是通过招摇和使用他们的接口我可以发送和接收数据,但我不知道如何在代码中实际实现它。下面是通过 swagger 从 API 生成的输出:
卷曲
curl -X POST --header 'Content-Type: application/json' --header 'Accept: application/json' --header 'Authorization: Bearer <XXXXXXXXXXXXX>' -d '{ \
"Sequence": "TGACCCCATATTGGGAGG", \
"NaConc": 50, \
"FoldingTemp": 25, \
"MgConc": 0, \
"NucleotideType": "DNA" \
}' 'https://www.idtdna.com/Restapi/v1/OligoAnalyzer/Hairpin'
响应正文
[
{
"id": "521e5459-236b-4443-9236-f6ade040ac3d",
"sequence": "CAGCAGTCATGCAATCG",
"thermo": 49.3,
"deltaS": -94.91,
"deltaG": -2.3,
"deltaH": -30.6,
"success": true,
"RNA": false,
"errorMessages": []
}
]
请求网址
https://www.idtdna.com/Restapi/v1/OligoAnalyzer/Hairpin
swagger interface swagger authorization
我尝试通过发送一个 post 请求并传入 clientID 和客户端密码作为身份验证以及数据输入字典在 python 中实现这一点。
import requests
import json
from requests_oauthlib import OAuth2Session
username = 'MyUsername'
password = 'MyPassword'
client_id = 'MyClientID'
client_secret = 'MySecret'
authorize_url = 'https://www.idtdna.com/IdentityServer/connect/authorize'
token_url = 'https://www.idtdna.com/IdentityServer/connect/token'
redirect_uri = 'https://www.idtdna.com/Restapi/swagger/docs/v1'
application_url = 'https://www.idtdna.com/Restapi/v1/OligoAnalyzer/Hairpin'
HairPinRequest = {
'Sequence': 'CGATCGATCGAT',
'NaConc': 50,
'FoldingTemp': 25,
'MgConc': 0,
'NucleotideType': 'DNA'
}
oauth = OAuth2Session(client_id, redirect_uri=redirect_uri, scope=application_url)
token = oauth.fetch_token(
token_url,
authorize_url,
username=username,
password=password,
client_id=client_id,
client_secret=client_secret
)
response = oauth.post(application_url, data=HairPinRequest)
print(response.url)
print(response)
运行此代码会产生未经授权的客户端错误:
\DNAsequencer.py", line 38, in <module>
client_secret=client_secret
\Anaconda3\lib\site-packages\requests_oauthlib\oauth2_session.py", line 360, in fetch_token
self._client.parse_request_body_response(r.text, scope=self.scope)
\Anaconda3\lib\site-packages\oauthlib\oauth2\rfc6749\clients\base.py", line 421, in parse_request_body_response
self.token = parse_token_response(body, scope=scope)
\Anaconda3\lib\site-packages\oauthlib\oauth2\rfc6749\parameters.py", line 431, in parse_token_response
validate_token_parameters(params)
\Anaconda3\lib\site-packages\oauthlib\oauth2\rfc6749\parameters.py", line 438, in validate_token_parameters
raise_from_error(params.get('error'), params)
\Anaconda3\lib\site-packages\oauthlib\oauth2\rfc6749\errors.py", line 405, in raise_from_error
raise cls(**kwargs)
oauthlib.oauth2.rfc6749.errors.UnauthorizedClientError: (unauthorized_client)
所以我知道它正在访问 API,但没有正确验证。我仍然不确定是否正确的两件事是scope 和redirect_uri。我不确定要使用哪些地址或如何识别合适的地址。
如果有人可以帮助就如何正确实施这一点提供一些帮助或建议,我们将不胜感激。
【问题讨论】:
-
仅供参考,有
requests-oauthlib库来处理 OAuth 2 流。 requests.readthedocs.io/en/master/user/authentication/… -
感谢您将我指向此文档。我还没有取得成功,但我认为我在实现目标方面取得了一些进展。我编辑了帖子以反映更新。
标签: python api oauth-2.0 python-requests swagger