在Python中使用DocuSign获取令牌

问题描述 投票:0回答:1

我在一个项目中工作,我试图获取访问令牌以使用DocuSing 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_application_token(
            DOCUSIGN_CLIENT_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函数会获取它。

感谢所有人:)

python api jwt docusignapi
1个回答
0
投票

只需看一下代码,返回的是“ 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

您会看到他们如何传递account_id

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

© www.soinside.com 2019 - 2024. All rights reserved.