我正在使用truevault API实现“忘记密码”功能。现在,我已经按照Postman流程对请求进行了测试,并且可以正常工作,但是,当我开始使用axios进行编码时,它始终会引发有关身份验证的问题。我尝试了几种组合(逻辑组合,而不仅仅是随机疯狂)。
另外,值得一提的是,我能够从UI列出我的truevault用户(不仅是邮递员),并尝试模仿与发帖请求相同的原理,但没有用
这是对我有用的邮递员请求:
json主体如下
{
"name":"XXXXX password reset",
"sg_template_id":"XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXcf42",
"sg_api_key":"XX.XXXXXXXXXXXXXXXXXXXXXX.XXXXXX_XXXX_XXXXXXXXXXXXXXXXXXXXXXXXXXZftJo",
"user_email_value_spec":{
"system_field":"username"
},
"from_email_value_spec":{
"literal_value":"do-not-reply@XXXXXX.com"
},
"substitutions":{
"{{FIRST_NAME}}":{
"user_attribute":"first_name"
}
}
}
结果成功,
现在,当我尝试使用axios时,我一直收到auth错误。代码如下:
createPasswordResetFlow()
{
axios.defaults.headers.common["Authorization"] = "XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXX27"; //tv user API KEY
axios.defaults.headers.post["Content-Type"] = "application/json";
var request = axios.post("https://api.truevault.com/v1/password_reset_flows",
{
auth:
{
username: 'XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXX27',
password: ""
},
data:
{
"name": "XXXXX password reset",
"sg_template_id": "XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXcf42",
"sg_api_key": "XX.XXXXXXXXXXXXXXXXXXXXXX.XXXXXX_XXXX_XXXXXXXXXXXXXXXXXXXXXXXXXXZftJo",
"user_email_value_spec":
{
"system_field": "username"
},
"from_email_value_spec":
{
"literal_value": "do-not-reply@XXXXXX.com"
},
"substitutions":
{
"{{FIRST_NAME}}":
{
"user_attribute": "first_name"
}
}
}
})
.then((res) =>
{
console.log(res);
return res.data.users;
})
.catch(error =>
{
console.log('error', error);
return error;
});
}
也如前所述,我一直在研究和尝试,但是如果有人可以帮助我,这是徒劳的。
答案 0 :(得分:0)
与您共享的JS代码有两个问题,这些都是导致该问题的原因:
设置默认Auth标头的行如下所示:axios.defaults.headers.common["Authorization"] = "XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXX27"; //tv user API KEY
。请注意,授权标头被设置为API密钥,而不是HTTP Basic Auth标头值。如果要以这种方式设置默认的auth标头,则需要将其设置为base64(API_KEY:)
而不是API_KEY
。
根据axios docs,post方法具有签名.post(url, data, config)
。结果,您的代码正在发布一个看起来像{auth: ..., data: ...}
的JSON对象。
尝试删除设置授权标头的行,然后将发布调用更改为如下所示:
axios.post("https://api.truevault.com/v1/password_reset_flows",
{
"name": "XXXXX password reset",
"sg_template_id": "XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXcf42",
"sg_api_key": "XX.XXXXXXXXXXXXXXXXXXXXXX.XXXXXX_XXXX_XXXXXXXXXXXXXXXXXXXXXXXXXXZftJo",
"user_email_value_spec":
{
"system_field": "username"
},
"from_email_value_spec":
{
"literal_value": "do-not-reply@XXXXXX.com"
},
"substitutions":
{
"{{FIRST_NAME}}":
{
"user_attribute": "first_name"
}
}
}
}, {
username: 'XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXX27',
password: ""
})