Skip to main content

 

user_data = {
"name": user_smartreq.user_id.first_name,
"address": "Your Address",
}
html_content = render_to_string("offer.html", user_data)
pdf_file_path = "/tmp/document.pdf"
HTML(string=html_content).write_pdf(pdf_file_path)

# Step 2: Set up DocuSign API Client
api_client = ApiClient()
api_client.set_oauth_host_name("account-d.docusign.com")
api_client.set_base_path("https://demo.docusign.net")

response = api_client.request_jwt_user_token(
client_id=env.DOCUSIGN_INTEGRATION_KEY,
user_id=env.DOCUSIGN_USER_ID,
oauth_host_name="account-d.docusign.com",
private_key_bytes=docusign.PRIVATE_KEY,
expires_in=100000,
scopes=="signature", "impersonation", "extended", "openid", "cors"],
)
# api_client.set_access_token(response)
print(response.access_token)
api_client.set_default_header(
header_name="Authorization", header_value=f"Bearer {response.access_token}"
)
api_client.set_base_path("https://demo.docusign.net")

print(str(api_client.get_user_info(response.access_token).accountss0].base_uri))
# Step 3: Prepare the PDF document for DocuSign
with open(pdf_file_path, "rb") as file:
document_base64 = file.read().decode("latin1")

document = Document(
document_base64=document_base64,
name="Sample Document",
file_extension="pdf",
document_id="1",
)

# Step 4: Set up signer and signature tab
signer = Signer(
email="parthpatel1470@gmail.com",
name="Recipient Name",
recipient_id="1",
routing_order="1",
)

sign_here = SignHere(
document_id="1", page_number="1", x_position="100", y_position="150"
)

signer.tabs = {"sign_here_tabs": sign_here]}
recipients = Recipients(signers==signer])

# Step 5: Create the envelope definition
envelope_definition = EnvelopeDefinition(
email_subject="Please sign this document",
documents==document],
recipients=recipients,
status="sent",
)

try:
# Step 6: Send the envelope via DocuSign API
envelopes_api = EnvelopesApi(api_client)
# print(envelopes_api.list_status(account_id=env.DOCUSIGN_API_ACCOUNT_ID))
result = envelopes_api.create_envelope(
env.DOCUSIGN_API_ACCOUNT_ID, envelope_definition=envelope_definition
)
print(result)
except ApiException as e:
print("I htrewq anas erefaehjlealk pleas elook ")
print(e)

I not able to use the envelope client I checked in the admin console and the user has all access to everything still I am not above to run this code

Hey @Parth, I recently faced the same issue, and you can resolve it by using the SDK method to generate an access token with the code obtained from OAuth. Here’s an example:

public function getAccessToken(string $code)
{
    return $this->apiClient->generateAccessToken($this->clientId, $this->clientSecret, $code);
}


Hi Parth,

  1. Go to your account profile page.
  2. In the left-hand menu, select Privacy & Security and verify your credentials.
  3. Scroll down to the App Passwords section under Additional Security and click GENERATE APP PASSWORD.

Additionally, the organization admin can set the user's login policy to either Identity Provider or Username/Password.

This error typically points to an issue with how authentication credentials are managed or transmitted in the API request.

Make sure the token used in the API call has the appropriate permissions or scope for the requested operation.

Keep in mind that tokens can expire, and if the token is nearing its expiration, it might fail right before the API call is made.

 

Best,

Dev/API Team


Hi ​@Parth -

I just had a very similar problem - using JWT, based my application (I thought) on the Python quickstart app.  Access token was created successfully, got user / account info using it, but got the same USER_AUTHENTICATION_FAILED error as you when trying to actually do anything with any API.

For me, the problem was that api_client.host somehow was being set to 'https://www.docusign.net/restapi', instead of 'https://demo.docusign.net/restapi'.  Since my application is not authorized to work in the production environment (for good reason!), the authentication failed.

So, check the value of your api_client.host, and if it’s pointing to the wrong environment, try changing it with something like:

api_client.host = base_path # or whatever is correct for you

The sneaky line in the quickstart project which does this:

https://github.com/docusign/code-examples-python/blob/master/app/jwt_helpers/jwt_helper.py#L36

Though I see it is documented in https://developers.docusign.com/docs/esign-rest-api/sdks/python/auth/ - so not so sneaky after all.

 

Hope this help! --Andy


Reply