Search code examples
pythondocusignapiaccess-token

DocuSign API returns Unauthorized, client error


I am trying to obtain a list of all templates a certain user has in its docusign account. First of all, I obtain an application access token using the follwing function:

def get_token():


    DS_JWT = {
        "ds_client_id": "79b1adb9-xxxx-xxxx-xxxx-4450dd98a712",
        "ds_impersonated_user_id": "3300d1d8-xxxx-xxxx-xxxx-552b87311b0f",
        "private_key_file": "private.key",
        "authorization_server": "account-d.docusign.com"
    }

    private_key = get_private_key(DS_JWT["private_key_file"]).encode("ascii").decode("utf-8")
    api_client = ApiClient()
    ds_app = api_client.request_jwt_application_token(
        client_id=DS_JWT["ds_client_id"],
        oauth_host_name=DS_JWT["authorization_server"],
        private_key_bytes=private_key,
        expires_in=3600,
        scopes=["signature", "impersonation"]
    )

    return ds_app.access_token

Once I have the application access token, I am trying to obtain the list of templates:

def list_templates(token: str):

    get_params = {'search_text': 'Test_3'}
    get_headers = {'Authorization': token}
    get_r = requests.get(url="https://demo.docusign.net/restapi/v2.1/accounts/b24dee2d-xxxx-xxxx-xxxx-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()
    print(token)
    templates = list_templates(token=token)
    print(templates)


if __name__ == '__main__':
    main()

If I use an access token obtained manually and give it as input to the list_templates() function, I obtain the list of templates without any problem. But if I use the token obtained using the get_token() function, I obtain the following error:

Traceback (most recent call last):
  File "src/docusign_template_app/obtain_token_4.py", line 57, in <module>
    main()
  File "src/docusign_template_app/obtain_token_4.py", line 52, in main
    templates = list_templates(token=token)
  File "src/docusign_template_app/obtain_token_4.py", line 35, in list_templates
    get_r.raise_for_status()
  File "/home/lluis/.pyenv/versions/docusign-template-app_py38/lib/python3.8/site-packages/requests/models.py", line 1021, in raise_for_status
    raise HTTPError(http_error_msg, response=self)
requests.exceptions.HTTPError: 401 Client Error: Unauthorized for url: https://demo.docusign.net/restapi/v2.1/accounts/b24dee2d-xxxx-xxxx-xxxx-d9d81de867ab/templates?search_text=Test_3

Any suggestions are more than welcome! Thanks


Solution

  • Application access tokens are only useful for Admin API endpoints

    You would need a user access token which you can get by calling the request_jwt_user_token function