在Python中的DocuSign中获取令牌

2024-10-04 01:28:23 发布

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

我正在一个项目中工作,我正在尝试获取访问令牌以使用docusignapi,但是获取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

谢谢大家:)


Tags: selfclienttokenapitargetiftimeresponse
3条回答

使用过期令牌也可能导致此错误(使用刷新端点获取新令牌)

您必须使用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

增加

From the comment: I have changed the call to request_jwt_user_token and I get another token, but it still fails. The response is {"error":"internal_server_error","reference_id":"846114d0-1bcd-47a6-ba23-317049b54d00"}

答复:

您正在调用/oauth/userinfo API方法。但未包括授权标头

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

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

在您的情况下,SDK应该为您设置它。可能是您正在使用一个新的client对象、一个旧的SDK版本或其他问题

我刚下载了eg-01-python-jwt code examplerepo,效果很好。我建议您先下载示例应用程序并使其运行,然后根据需要更新应用程序

此外,请检查您正在使用的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
...

只要看看代码,返回的是“acct”,一本字典。所以您需要使用account['account_id']

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

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

您可以看到他们是如何传递帐户id的

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

相关问题 更多 >