Im trying to connect to the Docusign API using Google Apps Script, but I dont know how to do it. I got my Integration Key and also created a app password because I read in documentation that I needed it. I would like to know how can I get the access token and how to send envelopes using this API.
I tried with this but it doesnt work
This to get the token:
function obtencionToken() {
var payload = {
"grant_type": "password",
"client_id": PropertiesService.getScriptProperties().getProperty('client_id'),
"username": PropertiesService.getScriptProperties().getProperty('username'), ////The name of the app password I generated
"password": PropertiesService.getScriptProperties().getProperty('password'), //The password of the app password I generated
"scope": "api"
}
var data = generarQuery(payload)
var params = {
"method": 'post',
"headers":
{
"content-type": "application/x-www-form-urlencoded",
},
"payload": JSON.stringify(data),
"muteHttpExceptions": true
}
var response = UrlFetchApp.fetch('https://demo.docusign.net/restapi/v2/oauth2/token', params);
console.log(params);
console.log(response);
console.info(response.getResponseCode())
console.info(response.getContentText())
var access_token_response = JSON.parse(response).access_token;
console.log("access_token_response: " + access_token_response)
/* save token in Propertyservice */
PropertiesService.getScriptProperties().setProperty('token', access_token_response);
return access_token_response;
}
The error of first function is: Error función 1
This to sent an envelope
function generarQuery(){
var payload = {
//"documents": docs,
"emailSubject": "Request a signature via email example",
"templateId": "<TemplateID>",
"recipients": {
"signers": [
{
"email": full_name,
"name": email_address,
"recipientId": "1",
"routingOrder": "1",
"pageNumbers": "1",
"tabs": {
"signHereTabs": [
{
"anchorString": "Firma Solicitante",
"anchorXOffset": "6.5",
"anchorYOffset": "-0.2",
"anchorIgnoreIfNotPresent": "false",
"anchorUnits": "cms",
}
],
},
}
],
},
"status": "sent"
}
var options2 = {
"method": "post",
"headers":
{
"Authorization": "Bearer"+ token2,
"content-type": "application/json"
},
"payload": JSON.stringify(payload),
"muteHttpExceptions": true
};
var token2 = UrlFetchApp.fetch('https://demo.docusign.net/restapi/v2/accounts/<AccountId>/envelopes', options2);
var id_sobre = (JSON.parse(token2).envelopeId);
console.log(payload);
console.log(options2);
console.log(id_sobre);
console.info(token2.getResponseCode())
console.info(token2.getContentText())
Logger.log(token2)
Logger.log("ID: " + id_sobre)
return id_sobre;
}
The error of second function is: Error función 2
There should be a space after "Bearer", so this is fixed code: