Lous
Lous

Reputation: 45

Incorrect access token with Authorization Code Grant, DocuSign

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

Upvotes: 0

Views: 179

Answers (1)

Sylvain
Sylvain

Reputation: 11

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.

Upvotes: 1

Related Questions