【发布时间】:2022-08-16 18:15:40
【问题描述】:
我正在尝试使用 oauth2 授权获取令牌,因为我需要将它用于自动化项目。
按照 DocuSign 网页教程 (https://developers.docusign.com/platform/auth/authcode/authcode-get-token/) 中的第 1 步,我有以下代码:
get_params = {\'response_type\': \'code\', \'scope\': \'signature\', \'client_id\': \'my_client_id\', \'redirect_uri\': \'https://localhost:3000\'}
get_r = requests.get(url=\"https://account-d.docusign.com/oauth/auth?\", params=get_params)
get_r.raise_for_status()
print(get_r.text)
我获得的响应是 HTML,但我想要带有授权码的 URL。
我见过类似的问题 (Python Requests library redirect new url),但似乎没有一个对我有用。
如何获取带有授权码的 URL?
编辑:现在我已经实现了以下代码,它返回一个令牌。
from os import getenv
from typing import List
import requests
from dotenv import load_dotenv
from oauthlib.oauth2 import BackendApplicationClient
from requests_oauthlib import OAuth2Session
def get_token() -> str:
\"\"\"Get access token from Docusign API using a client ID and its secret.
More info on https://requests-oauthlib.readthedocs.io/en/latest/oauth2_workflow.html#backend-application-flow
\"\"\"
client_id = getenv(\"DOCUSIGN_CLIENT_ID\")
client_secret = getenv(\"DOCUSIGN_CLIENT_SECRET\")
token_url = getenv(\"DOCUSIGN_TOKEN_URL\")
client = BackendApplicationClient(client_id=client_id)
oauth = OAuth2Session(client=client)
token = oauth.fetch_token(
token_url=token_url,\\
client_id=client_id,
client_secret=client_secret
)
return \"Bearer \" + token[\"access_token\"]
我正在尝试使用此令牌返回某个用户(与我们刚刚获得的给定令牌相对应)拥有的模板列表。
def list_templates(token: str) -> List[str]:
\"\"\"\" Make a list of all the templates that a user, that corresponds to the token proportioned as input, has. \"\"\"
get_params = {\'search_text\': \'Test_3\'}
get_headers = {\'Authorization\': token}
get_r = requests.get(url=\"https://demo.docusign.net/restapi/v2.1/accounts/b24dee2d-ca55-41d0-996c-d9d81de867ab/templates\", params=get_params, headers=get_headers)
get_r.raise_for_status()
data = get_r.json()
data_templates = data[\'envelopeTemplates\']
list_templates = []
for inner_data in data_templates:
for relevant_data_key, relevant_data_value in inner_data.items():
if relevant_data_key == \'name\':
list_templates.append(relevant_data_value)
return list_templates
def main():
load_dotenv(dotenv_path=\".env\", override=True, verbose=True)
token = get_token()
templates = list_templates(token=token)
if __name__ == \'__main__\':
main()
但我似乎令牌无效。 另一方面,当手动获取令牌并将其用作输入时,它可以完美运行!
有人知道为什么我没有获得正确的令牌吗?
谢谢 :)
标签: python python-requests docusignapi