【问题标题】:Retrieving Oauth2 token in swagger and interfacing with API using Python大摇大摆地检索 Oauth2 令牌并使用 Python 与 API 交互
【发布时间】: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,但没有正确验证。我仍然不确定是否正确的两件事是scoperedirect_uri。我不确定要使用哪些地址或如何识别合适的地址。

如果有人可以帮助就如何正确实施这一点提供一些帮助或建议,我们将不胜感激。

【问题讨论】:

标签: python api oauth-2.0 python-requests swagger


【解决方案1】:

如果 OP 或其他任何人仍然觉得这很有用,这是我对该问题的解决方案。我有一个供应商 API 帐户,并且能够获得所需的响应。 我仍在尝试找出其他工具。

#! /usr/bin/python3

import requests
import json
from requests_oauthlib import OAuth2Session

username = 'redacted'
password = 'redacted'
client_id = 'redacted'
client_secret = 'redacted'
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'
scope = 'test'

HairPinRequest = {
    'Sequence': 'CGATCGATCGAT',
    'NaConc': 50,
    'FoldingTemp': 25,
    'MgConc': 0,
    'NucleotideType': 'DNA'
}

#This token request code does work
payload = "grant_type=password&client_id=redacted&client_secret=redacted&username=redacted&password=redacted&scope=test"

headers = { 'accept': "application/json", 'content-type': "application/x-www-form-urlencoded" }
response = requests.request("POST", token_url, data=payload, headers=headers)
#This prints a text version
print(response.text)
#This creates a json object and allows access_token to be used as variable
j = response.json()

headers = {"Authorization": "Bearer " +j['access_token'] }

响应是一个json对象

{"access_token":"redacted","expires_in":3600,"token_type":"Bearer"} [{'id':'2a139206-3e86-4eb6-9d77-0a14bdde73b5','序列': 'CGATCGATCGAT','thermo':47.1,'deltaS':-80.88,'deltaG':-1.79, 'deltaH':-25.9,'成功':真,'RNA':假,'errorMessages':[]}]

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-05-05
    • 1970-01-01
    • 2017-10-11
    • 2015-12-14
    • 1970-01-01
    • 1970-01-01
    • 2019-05-11
    • 2022-09-28
    相关资源
    最近更新 更多