在ExpressJS中创建身份验证标头。Java到Javascript的翻译
在ExpressJS中创建身份验证标头。Java到Javascript的翻译,javascript,java,express,post,header,Javascript,Java,Express,Post,Header,为了使用另一个API对我的节点服务器(Express JS)进行身份验证,必须在POST请求中发送一个单独的头 引用文件: { "installationId":"<INSTALLATION_ID>", "nonce":"0wYWLarLDBrWU7B2I1Go4A==", "timestamp":"2018-11-01T11:32:44.413Z" } 您需要随此请求发送X-Authorization-Ahoi头。这个 标头值是一个编码的JSON字符串,由
为了使用另一个API对我的节点服务器(Express JS)进行身份验证,必须在POST请求中发送一个单独的头 引用文件:
{
"installationId":"<INSTALLATION_ID>",
"nonce":"0wYWLarLDBrWU7B2I1Go4A==",
"timestamp":"2018-11-01T11:32:44.413Z"
}
您需要随此请求发送X-Authorization-Ahoi头。这个
标头值是一个编码的JSON字符串,由
,一个随机的16个字符的字符串,作为nonce和
ISO 8601格式的当前日期
文档中的示例:
{
"installationId":"<INSTALLATION_ID>",
"nonce":"0wYWLarLDBrWU7B2I1Go4A==",
"timestamp":"2018-11-01T11:32:44.413Z"
}
现在我的问题是:创建Nonce有什么问题,如何修复它?
还是我将POST请求发送到的API?您刚刚忘记将
非EBASE64ENC
转换为字符串。您已经用java实现了这一点,但没有用javascript
只需添加.toString('hex')代码>在非EBASE64ENC的末尾
示例:const nonceBase64Enc=Buffer.from(nonce,'base64')。toString('hex')
你现在完成了!这正是解决我问题的方法。非常感谢你!
const installationId = '<EXAMPLE_INSTALLATIONID>';
const nonce_tmp = crypto.randomBytes(16);
const nonce = nonce_tmp.toString('base64');
const date = new Date();
const timestamp = date.toISOString();
const xAuthAhoiJson = JSON.stringify({installationId, nonce, timestamp});
const XAUTH_AHOI_BASE64_URL_ENC = base64url.encode(xAuthAhoiJson);
const url = 'https://banking-sandbox.starfinanz.de/auth/v1/oauth/token?grant_type=client_credentials';
const options = { headers:
{ 'Authorization': 'Basic ' + Buffer.from(credentials).toString('base64'),
'X-Authorization-Ahoi': XAUTH_AHOI_BASE64_URL_ENC }
};
request.post(url, options, function (e, r, b) { console.log(b); });
{
"timestamp": 1543229480764,
"status": 400,
"error": "Bad Request",
"message": "Nonce is invalid.",
"path": "/auth/v1/oauth/token"
}