在swagger中检索Oauth2令牌并使用Python与API接口

2024-10-01 17:38:32 发布

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

我是python新手,我正在尝试编写一个简单的程序,将DNA序列发送到API并返回一些简单的参数。据我所知,这使用了Oauth2.0身份验证,因此我需要使用我的clientID和客户端机密请求一个令牌,然后通过post请求将该令牌与数据一起传递,以接收所需的输出。API是通过swagger和使用他们的接口,我可以发送和接收数据,但我不知道如何在代码中实际实现这一点。下面是API通过swagger生成的输出:

卷曲

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": []
  }
]

请求URL

https://www.idtdna.com/Restapi/v1/OligoAnalyzer/Hairpin

swagger interfaceswagger authorization

我试图在python中实现这一点,方法是发送一个post请求,传入clientID和client secret作为身份验证以及数据输入的字典

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。我不确定使用什么地址,也不知道如何识别合适的地址

如果有人能提供一些帮助或建议,如何正确地实施这一点,将不胜感激


Tags: inpyhttpscomclienttokenurlsecret
1条回答
网友
1楼 · 发布于 2024-10-01 17:38:32

如果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', 'sequence': 'CGATCGATCGAT', 'thermo': 47.1, 'deltaS': -80.88, 'deltaG': -1.79, 'deltaH': -25.9, 'success': True, 'RNA': False, 'errorMessages': []}]

相关问题 更多 >

    热门问题