Obtain Access token to Docusign using Google Apps Script

Viewed 145

I am tying to obtain Access Token using Google Apps Script.

Ref: https://developers.docusign.com/platform/auth/jwt/jwt-get-token/

Using Postman, I can get the ACCESS TOKEN by posting a request using this URL: https://account-d.docusign.com/oauth/token and this two fields in the body:

  • grant_type : urn:ietf:params:oauth:grant-type:jwt-bearer
  • assertion : MY_JSON_WEB_TOKEN

How do I replicate tis in Apps script?
After endless hours of trial and error I still get this error: {"error":"invalid_grant","error_description":"unsupported_grant_type"}

The script:

function obtainAccessToken(){
  const JWT = MY_JSON_WEB_TOKEN;

  const URL = 'https://account-d.docusign.com/oauth/token';

  const PARAMS = {
    "muteHttpExceptions": true,
    "method": "POST",
    "contentType": "application/json",
    "payload": {
      "body":{
        "grant_type": "urn:ietf:params:oauth:grant-type:jwt-bearer",
        "assertion": JWT
      }
    } 
  };

  return UrlFetchApp.fetch(URL, PARAMS);
}
1 Answers

This is the correct function:

function obtainAccessToken(){
  const JWT = MY_JSON_WEB_TOKEN;

  const URL = 'https://account-d.docusign.com/oauth/token';

  const PARAMS = {
    "method": "POST",
    "headers":{
      "Accept": "application/json",
      "contentType": "application/x-www-form-urlencoded"
    },
    "payload": {
      "grant_type": "urn:ietf:params:oauth:grant-type:jwt-bearer",
      "assertion": JWT
    } 
  };

  return UrlFetchApp.fetch(URL, PARAMS);
}
Related