如何使用Google Apps Script获取Docusign Access Token并解决授权报错
问题原因
你的代码存在两处核心错误,直接触发了unsupported_grant_type报错:
- 错误设置请求头
Content-Type为application/json,DocuSign OAuth令牌端点仅接收application/x-www-form-urlencoded格式的请求体,这也是你用Postman能成功、代码报错的核心差异 - Payload结构多嵌套了不必要的
body层,Google Apps Script的UrlFetchApp方法中,payload字段直接传入表单键值对即可,不需要额外封装body对象
修正后代码
function obtainAccessToken(){ const JWT = "替换为你实际的JWT字符串"; const URL = 'https://account-d.docusign.com/oauth/token'; const PARAMS = { "muteHttpExceptions": true, "method": "POST", // 不需要手动指定contentType,UrlFetchApp传入对象格式的payload会自动设置为x-www-form-urlencoded "payload": { "grant_type": "urn:ietf:params:oauth:grant-type:jwt-bearer", "assertion": JWT } }; const response = UrlFetchApp.fetch(URL, PARAMS); // 可直接解析返回的令牌信息 console.log(response.getContentText()); return response; }
如果调试时还有其他错误,可检查JWT的有效期、签名密钥、用户授权范围是否配置正确。
内容的提问来源于stack exchange,提问作者AttiER
相关产品推荐
相关产品推荐

