【问题标题】:How to get an access token with Authorization Code Grant, DocuSign如何使用授权码授予、DocuSign 获取访问令牌
【发布时间】: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


    【解决方案1】:

    OAuth 授权码授权流程很复杂。安全专家建议开发人员应该不是如果有可用的库,请自行实现。

    有关建议使用的库,请参阅 Ben 的回答。

    此外:

    1. 您需要提供您的客户端 ID(集成密钥)。您的代码显示您提供的是字符串“my_client_id”。
    2. 应该正在取回 html——这是用户将完成的身份验证页面。换句话说,授权代码授权流程的第一步是将用户的浏览器重定向到您构建的 URL。您的代码似乎正在向身份验证 URL 发出自己的 GET 请求,这是不对的。
    3. 您缺少state 参数。虽然它不是必需的,但安全专家建议将其包含在内,并带有 nonce 值。您的应用程序还应该存储状态值,并将其与您将授权代码交换为访问令牌时返回的值进行比较。这可以防止一种类型的攻击。

    【讨论】:

      【解决方案2】:

      最简单的方法是使用Request-OAuthlib Oauth 2 工作流程的完整示例。

      【讨论】:

        猜你喜欢
        • 2022-08-18
        • 1970-01-01
        • 1970-01-01
        • 2020-09-13
        • 2018-12-20
        • 1970-01-01
        • 1970-01-01
        • 2019-03-21
        • 2018-06-13
        相关资源
        最近更新 更多