0

Im trying to get the token with oauth2 authorization since I will need it to an automation project.

I have been told that the easiest way to do this is to use: https://requests-oauthlib.readthedocs.io/en/latest/

Right now I have implemented the following code, which returns an incorrect token.

from os import getenv
from typing import List

import requests
from dotenv import load_dotenv
from oauthlib.oauth2 import BackendApplicationClient
from requests_oauthlib import OAuth2Session

def get_token() -> str:
    """Get access token from Docusign API using a client ID and its secret.

    More info on https://requests-oauthlib.readthedocs.io/en/latest/oauth2_workflow.html#backend-application-flow
    """
    client_id = getenv("DOCUSIGN_CLIENT_ID")
    client_secret = getenv("DOCUSIGN_CLIENT_SECRET")
    token_url = getenv("DOCUSIGN_TOKEN_URL")
    client = BackendApplicationClient(client_id=client_id)
    oauth = OAuth2Session(client=client)
    token = oauth.fetch_token(
        token_url=token_url,\
        client_id=client_id,
        client_secret=client_secret
    )
    return "Bearer " + token["access_token"]

I am trying to use this token to return the list of the templates a certain user (that corresponds with the given token we have just obtained) has.

def list_templates(token: str) -> List[str]:
    """" Make a list of all the templates that a user, that corresponds to the token proportioned as input, has. """

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


if __name__ == '__main__':
    main()

NOTE: In function list_templates(), when doing the GET request, we must put the correct API account ID in the URL

But I seems that the token is not valid and therefore I can not create the list of templates a certain user has.

On the other hand, when obtaining the token manually and using it as an imput, it works perfectly!

Does someone know why I am not obtaining the correct token?

Thanks :)

Lous
  • 35
  • 6
  • What is token_url = getenv("DOCUSIGN_TOKEN_URL")? – Inbar Gazit Aug 17 '22 at 15:12
  • token_url corresponds to: https://account-d.docusign.com/oauth/token – Lous Aug 18 '22 at 07:36
  • what error message do you get? – Inbar Gazit Aug 18 '22 at 15:32
  • 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: 400 Client Error: Bad Request for url: https://account-d.docusign.com/oauth/token – Lous Aug 19 '22 at 07:46
  • this is a duplicate of https://stackoverflow.com/questions/73331749/how-to-get-an-access-token-with-authorization-code-grant-docusign – Inbar Gazit Aug 24 '22 at 20:00
  • 1
    Does this answer your question? [How to get an access token with Authorization Code Grant, DocuSign](https://stackoverflow.com/questions/73331749/how-to-get-an-access-token-with-authorization-code-grant-docusign) – Inbar Gazit Aug 24 '22 at 20:01

1 Answers1

1

Take into account you can easily have a Python environment ready to use with Authorization Code Grant by using the Quickstart process given here: https://developers.docusign.com/docs/esign-rest-api/quickstart/ https://developers.docusign.com/docs/esign-rest-api/quickstart/overview/

If you wanted to keep your code anyway, thanks to open a Support case through the web form or by logging in to the Support Site and selecting Create a case. It will allow to share Ids used in your scenario in order to troubleshoot your code.

Sylvain
  • 11
  • 1