我在尝试交换访问令牌代码时遇到错误。声明无效授权。不知道我在这里做错了什么。
以下是代码:
var integratorId = 'xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx';
var secretKey = 'xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx';
var basic = base64.encode(integratorId + ':' + secretKey);
console.log(basic);
var JWTheader = {"typ":"JWT", "alg":"RS256"};
var JWTbody = {
"iss":"bfcb8406-4994-4715-8f6d-112f5670db2d",
"sub":"832f391a-a3f7-4c29-b8f0-3226c06accd2",
"aud": "account-d.docusign.com",
"scope": "signature"
};
var unSignedToken = base64.encode(JWTheader) + '.' + base64.encode(JWTbody);
var cert = fs.readFileSync('/home/toplevelstaging/TopLevelAPI/routes/fs_node/RSAkey.pem');
var token = jwt.sign({unSignedToken, iat:Math.floor(Date.now()/100),exp:Math.floor(Date.now()/1000)+60}, cert, {algorithm:'RS256'});
var baseUrl = 'https://demo.docusign.net/restapi/v2/accounts/3465212';
var oauth = 'https://account-d.docusign.com/oauth/token';
var tokenUrl = 'grant_type=urn:ietf:params:oauth:grant-type:jwt-bearer&assertion='+token;
request({
headers:{"Content-Type":"application/x-www-form-urlencoded","Authorization":"Basic "+basic},
url:oauth,
json: true,
method: "POST",
data: tokenUrl,
},function(err,response,body){
console.log(response);
}
);
以下是我收到的错误:
body: { error: 'invalid_grant' } }
答案 0 :(得分:1)
您使用的是哪个JWT库?
JWT库应该为您创建JWT - 您不应该处理令牌部分的base64编码。
在您的示例中,您似乎手动滚动JWT的部分,然后使用库进行签名。
寻找一个能够为您处理一切的更好的库。有关更多提示,请参阅我的blog post和食谱。