在Python中的DocuSign中获取令牌

时间:2020-01-09 15:30:38

标签: python api jwt docusignapi

我在一个项目中工作,我试图获取访问令牌以使用DocuSign API,但是获取oauth userinfo的调用无效。

代码:

class BaseDocusign:
    api_client = None
    _token_received = False
    expiresTimestamp = 0
    account = None

    def __init__(self):
        BaseDocusign.api_client = ApiClient()

    def token_is_expired(self):
        current_time = int(round(time.time()))
        return (current_time + DOCUSIGN_EXPIRE_TIME - 10) > BaseDocusign.expiresTimestamp

    def check_token(self):
        if not BaseDocusign._token_received or self.token_is_expired():
            self.update_token()

    def update_token(self):
        client = BaseDocusign.api_client
        client.request_jwt_user_token(
            DOCUSIGN_CLIENT_ID,
            DOCUSIGN_ACCOUNT_ID,
            DOCUSIGN_AUTH_SERVER,
            DOCUSIGN_PRIVATE_KEY,
            DOCUSIGN_EXPIRE_TIME
        )
        if BaseDocusign.account is None:
            account = self.get_account_info(client)
            print account

        BaseDocusign._token_received = True
        BaseDocusign.expiresTimestamp = (int(round(time.time())) + DOCUSIGN_EXPIRE_TIME)

    def get_account_info(self, client):
        client.host = DOCUSIGN_AUTH_SERVER
        response = client.call_api("/oauth/userinfo", "GET", response_type="object")

        if len(response) > 1 and 200 > response[1] > 300:
            raise Exception("can not get user info: %d".format(response[1]))

        accounts = response[0]['accounts']
        target = target_account_id

        if target is None or target == "FALSE":
            # Look for default
            for acct in accounts:
                if acct['is_default']:
                    return acct

        # Look for specific account
        for acct in accounts:
            if acct['account_id'] == target:
                return acct

        raise Exception("User does not have access to account {target}\n")

运行时:

a = BaseDocusign()
a.update_token()

生成访问令牌:

{"access_token":"eyJ0eXAiOiJNVCIsImFsZyI6IlJTMjU2Iiwia2lkIjoiNjgxODVmZjEtNGU1MS00Y2U5LWFmMWMtNjg5ODEyMjAzMzE3In0.AQkAAAABAAsADQAkAAAAZjczYjYxMmMtOGI3Ny00YjRjLWFkZTQtZTI0ZWEyYjY4MTEwIgAkAAAAZjczYjYxMmMtOGI3Ny00YjRjLWFkZTQtZTI0ZWEyYjY4MTEwBwAAq89LFJXXSAgAAOvyWVeV10gLAB8AAABodHRwczovL2FjY291bnQtZC5kb2N1c2lnbi5jb20vDAAkAAAAZjczYjYxMmMtOGI3Ny00YjRjLWFkZTQtZTI0ZWEyYjY4MTEwGAABAAAABQAAABIAAQAAAAYAAABqd3RfYnI.f_XW63iL5ABts-gq48ciWKQnaYyNiIEG9rC_CpnyWo0Hzf-B_G3hIRUWJzD1Yiyyy4pKm_8-zoalsoqANcMeXsjwBTCMlXIhc216ZWa6nHR6CheRbfTHM6bJ1LKwRdmnpwLywu_qiqrEwEOlZkwH_GzSSP9piUtpCmhgdZY1GFnG2u9JU_3jd8nKN87PE_cn2sjD3fNMRHQXjnPeHPyBZpC171TyuEvQFKCbV5QOwiVXmZbE9Aa_unC-xXvvJ2cA3daVaUBHoasXUxo5CZDNb9aDxtQkn5GCgQL7JChL7XAfrgXAQMOb-rEzocBpPJKHl6chBNiFcl-gfFWw2naomA","token_type":"Application","expires_in":28800}

但是当尝试获取帐户信息时,呼叫失败:

{"error":"internal_server_error","reference_id":"f20e360c-185d-463e-9f0b-ce95f38fe711"}

为此,我调用了get_account_info函数,并调用了端点oauth/userinfo,但是调用失败。

response = client.call_api("/oauth/userinfo", "GET", response_type="object")
# Response: {"error":"internal_server_error","reference_id":"f20e360c-185d-463e-9f0b-ce95f38fe711"}

要执行此操作example,我需要变量account_id,根据此示例,get_account_info函数可以获取它。

我也试图按照web所说的(步骤4)获取用户信息,答案是:

curl --request GET https://account-d.docusign.com/oauth/userinfo--header "Authorization: Bearer eyJ0eXAiOiJNVCIsImFsZyI6IlJTMjU2Iiwia2lkIjoiNjgxODVmZjEtNGU1MS00Y2U5LWFmMWMtNjg5ODEyMjAzMzE3In0.AQoAAAABAAUABwAAYWSFlJrXSAgAAMko55ya10gCAP-ftnA70YROvfpqFSh7j7kVAAEAAAAYAAEAAAAFAAAADQAkAAAAZjczYjYxMmMtOGI3Ny00YjRjLWFkZTQtZTI0ZWEyYjY4MTEwIgAkAAAAZjczYjYxMmMtOGI3Ny00YjRjLWFkZTQtZTI0ZWEyYjY4MTEwEgABAAAABgAAAGp3dF9iciMAJAAAAGY3M2I2MTJjLThiNzctNGI0Yy1hZGU0LWUyNGVhMmI2ODExMA.YHFoD2mQbwh8rdiPi8swg9kO9srlDyJcpqUo8XI5tdZki2I_Nla-qb9VaD4gAy8tSXVSY7unRjfClFDAqC8Ur73caHuZo7tN5tIKmXi6C3VzPWPGFJtsceKNEGMqwznw6OBVuPQG0IGlRjXK37Ur1nILLUWKb7w6O5Uz6y0e5uR8sxzZWh1adm2zHqd6khiQuAFB9vG2sS3jaudtck1qV6HRB_kARvUie1zglvHydc42Nc_o5GtIm3sGrqW7rio3YpHVX39nTKM-28kjOvPSNwzXp3IlZtaxuB6EdexrECH19nIaNbCe29LrdpzreRMyjEwwM309bOaKJ1KV82NbTQ"

# Response
<html><head><title>Object moved</title></head><body>
<h2>Object moved to <a href="https://account-d.docusign.com/">here</a>.</h2>
</body></html>
curl: (3) URL using bad/illegal format or missing URL

感谢所有人:)

3 个答案:

答案 0 :(得分:1)

只要看一下代码,返回的就是“ acct”,就是一本字典。因此,您需要使用<link rel=stylesheet href="https://stackpath.bootstrapcdn.com/bootstrap/4.4.1/css/bootstrap.min.css"> <div> <nav class="navbar"> <div class="img"> <img src="..."> </div> <div class="img"> <img src="..."> </div> </nav> </div> <div class="container"> <div class="row"> <div class="col-md-3" style="font-size:16px"> </div> <div class="col-md-3 border-left"> </div> <div class="col-md-3 border-left"> </div> <div class="col-md-3 border-left"> </div> </div> </div>

我找到了完整的示例:https://github.com/docusign/eg-01-python-jwt

在这里:https://github.com/docusign/eg-01-python-jwt/blob/master/example_base.py#L44

您会看到他们如何传递account_id

希望这会有所帮助。祝你好运

答案 1 :(得分:0)

您必须使用request_jwt_user_token而不是request_jwt_application_token

请参见代码示例:https://github.com/docusign/eg-01-python-jwt/blob/master/example_base.py#L34

request_jwt_application_token仅适用于某些DocuSign组织API。

已添加

从评论中: 我已将调用更改为request_jwt_user_token,但又获得了另一个令牌,但仍然失败。响应为{“ error”:“ internal_server_error”,“ reference_id”:“ 846114d0-1bcd-47a6-ba23-317049b54d00”}

答案:

您正在调用/ oauth / userinfo API方法。但是不包括Authorization标头。

一种方法是显式设置授权:

client.set_default_header("Authorization", "Bearer " + ds_access_token)

对于您而言,SDK应该为您设置。可能是您使用的是新的client对象,较旧的SDK版本或其他问题。

我刚刚下载了eg-01-python-jwt code example回购,并且工作正常。我建议您下载示例应用程序并使其首先运行,然后根据需要更新该应用程序。

另外,检查您使用的Python SDK的版本:

pip3 show docusign_esign
Name: docusign-esign
Version: 3.0.0
Summary: DocuSign REST API
...
Location: /usr/local/lib/python3.7/site-packages
...

答案 2 :(得分:0)

此错误也可能是由于使用了过期的令牌(使用刷新端点来获取新令牌)引起的。