正在DocuSign-Node.js中请求访问令牌
我遵循以下文件: 我正在为回调API尝试以下操作:正在DocuSign-Node.js中请求访问令牌,node.js,docusignapi,Node.js,Docusignapi,我遵循以下文件: 我正在为回调API尝试以下操作: router.get('/authGrantReturn', function(req, res){ let query = req.query; console.log("code: " + req.query.code); console.log("status: " + req.query.state); console.log("in authGrantReturn"); retur
router.get('/authGrantReturn', function(req, res){
let query = req.query;
console.log("code: " + req.query.code);
console.log("status: " + req.query.state);
console.log("in authGrantReturn");
return new Promise((resolve, reject)=>{
try{
let bodyData = {
grant_type: 'authorization_code',
code: req.query.code
};
console.log( "========code======")
console.log( req.query.code)
console.log( "========/code======")
let combination = `${integrationKey}:${secretKey}`;
let b64Combination = Buffer.from(combination).toString('base64');
console.log("combination "+ combination)
console.log("b64Combination "+ b64Combination)
fetch('https://account-d.docusign.com/oauth/token',{
method: 'POST',
headers: { 'Authorization': 'Basic ' + b64Combination,
'Content-Type': 'application/x-www-form-urlencoded' },
body: bodyData
})
/* fetch('https://account-d.docusign.com/oauth/token?grant_type:authorization_code&code='+b64Combination,{
method: 'GET',
headers: { 'Authorization': 'Basic ${b64Combination}', 'Content-Type': 'application/x-www-form-urlencoded', 'Content-Encoding': 'gzip'}
}) */
.then(function (res) {
等等
上面是url调用的回调函数,如下所示:
然而,我得到了“坏请求”,req.query.code是正确的,我不确定组合
我遗漏了什么?我对您的代码进行了目视检查,并提出了几点意见: 可读性 我建议您使用wait,而不是显式地处理promise对象。请注意,您的代码需要在
async
函数中才能使用wait,但这很容易做到
通过Fetch发送表单数据
我认为您的具体问题是没有正确发送表单数据。看
警告:以下代码使用FormData对象。它总是创建多部分/表单数据
请求。如果这不起作用,那么您可以通过URLSearchParams对象或显式地创建表单数据。请参阅参考资料。
未测试代码:
const bodyform = new FormData();
bodyform.append('grant_type', 'authorization_code');
bodyform.append('code', 'req.query.code');
console.log( "========code======")
console.log( req.query.code)
console.log( "========/code======")
let combination = `${integrationKey}:${secretKey}`;
let b64Combination = Buffer.from(combination).toString('base64');
console.log("combination "+ combination);
console.log("b64Combination "+ b64Combination);
let res = await fetch('https://account-d.docusign.com/oauth/token',{
method: 'POST',
headers: { 'Authorization': 'Basic ' + b64Combination },
body: bodyform
});
console.log( "========Results========"); console.log (res);
检查状态
检查从DocuSign返回的状态
值是否与将用户浏览器重定向到DocuSign标识服务时发送的值相同,这一点很重要。状态值。它必须是一个新的nonce值--随机且不可用
尝试/抓住
您应该将fetch调用包装在try/catch块中。这将在等待捕捉错误时正常工作
使用图书馆
安全专家建议您在实现与安全相关的过程时使用库(库通常可用)
对于Node.js,这个库非常流行,并且有一个用于DocuSign的函数。看看这是如何使用它的
请参阅和获取用户名、帐户id、API基url等的支持对象。谢谢Larry,我正在研究您的git示例。另一个问题:有时返回的“html字符串”包含同意/接受表单,如何向用户显示?我试过res.writehead()和res.send();我不确定这是否是从该API获得的正确响应。根据stackoverflow策略,请以新问题的形式询问其他问题。StackOverflow不是留言板。谢谢欢迎来到StackOverflow!请投票选出所有有用的答案,包括对他人问题的答案。请务必检查/接受您自己问题的最佳答案!非常感谢。。。。