Get tokens in DocuSign in Python

docusign sandbox
docusign rest api
docusign no_valid_keys_or_signatures
docusign access token
docusign refresh token
docusign api python
docusign invalid_grant
docusign jwt

I am working in a project and I am trying to get the access token to use the DocuSign API, but the call to get the oauth userinfo does not work.

The code:

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")

When I run it:

a = BaseDocusign()
a.update_token()

The access token is generated:

{"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}

But when try to get the account info, the call fails:

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

To do this, I call to the get_account_info function and it calls to the endpoint oauth/userinfo, but the call fails.

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

To do this example, I need the variable account_id and according to this example, the get_account_info function gets it.

I have also tried to do what the web says (step4) to get user information and the answer is:

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

Thanks for all :)

Just looking into the code, the return is "acct", a dictionary. So you need to use account['account_id']

I found this full example: https://github.com/docusign/eg-01-python-jwt

And in here: https://github.com/docusign/eg-01-python-jwt/blob/master/example_base.py#L44

you see how they are passing the account_id

Hopefully this helps. good luck

Get tokens in DocuSign in Python, token to use the DocuSing API, but the call to get the oauth userinfo does not work. Authentication type: JWT Grant Python SDK: docusign- DocuSign just released two new SDKs: Python and Ruby. Each enable you to seamlessly integrate with the DocuSign eSignature API.With just a few lines of code you can automate workflows such as requesting signatures or signing documents directly inside your Python or Ruby apps.

You must use request_jwt_user_token not request_jwt_application_token

See the code example: https://github.com/docusign/eg-01-python-jwt/blob/master/example_base.py#L34

request_jwt_application_token is only for some of the DocuSign organization APIs.

Added

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"}

Answer:

You're calling the /oauth/userinfo API method. But the Authorization header was not included.

One way is to set the Authorization explicitly:

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

In your case, the SDK is supposed to set it for you. It could be that you're using a new client object, an older SDK version, or some other issue.

I just downloaded the eg-01-python-jwt code example repo and it worked fine. I suggest that you download the example app and get it running first, then update the app to your needs.

Also, check the version of the Python SDK you're using:

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

Authorization Code Grant Authentication, Step 2: Obtain the access token. Once you have an authorization code, use the authentication service /oauth/token endpoint to obtain access and refresh tokens. I am trying to get an access token to access the DocuSign Monitor API via the JWT grant method as it states in the documentation for the DocuSign Monitor API. Here is a snippet of my test script:

This error can be also caused by using an expired token (use the refresh end point to get a new one)

JSON Web Token Authentication, Find out how to use the DocuSign Authentication Service JSON Web Token for Python example project � Ruby example project � Bash example project See Obtaining Consent for details on how to obtain consent from individuals and use � The Quick Start examples are meant to be used with DocuSign's OAuth Token Generator, which will allow you to generate tokens for the Demo/Sandbox environment only. These tokens last for eight hours and will enable you to build your proof-of-concept application without the need to fully implement an OAuth solution.

docusign/docusign-python-client: The Official DocuSign , The Official DocuSign Python Client Library used to interact with the eSign These examples use either DocuSign's Authorization Code Grant or JSON Web Tokens (JWT) We also have an active developer community on Stack Overflow. <div style="background:red">Please enable javascript for this page</div><iframe src="https://www.googletagmanager.com/ns.html?id=GTM-WPK6FN5" height="0" width="0

docusign/code-examples-python: Python code examples, Contribute to docusign/code-examples-python development by creating an account on GitHub. Retrieve the tab (field) values for all of the envelope's recipients. Source. Authentication with DocuSign via the JSON Web Token ( JWT) Grant. This article explains how to obtain and use an Access Token from a Refresh Token in the Authorization Code Grant Flow for the Docusign REST API. I assume that the reader is aware of what Docusign

Docusign API Integration with Django WebApp, Wondering How to Integrate Docusign with Django? Now Envelope is created by DocuSign python-client containing Oauth2 token, pdf (generated For support only python code functions are provided, We have to code from the core with� Find out how to use the DocuSign Authentication Service JSON Web Token for service integrations not involving a user agent, such as a browser or web view control.

Comments
  • Thanks for answering, but the problem is when I try to get the account_id, the endpoint "/oauth/userinfo" fails. The response is {"error":"internal_server_error","reference_id":"f20e360c-185d-463e-9f0b-ce95f38fe711"}
  • 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"}