我正在尝试通过 node.js 后端应用程序使用 Hubspot 获取 OAuth 的访问令牌。我遵循以下描述:https://developers.hubspot.com/docs/api/oauth-quickstart-guide
为了获取访问令牌,我编写了以下方法:
function getAccessToken(code){
const formData = {
grant_type: 'authorization_code',
client_id: process.env.CLIENT_ID,
client_secret: process.env.CLIENT_SECRET,
redirect_uri: process.env.REDIRECT_URI,
code: code
};
axios.post('https://api.hubapi.com/oauth/v1/token',
{
form: formData
},
{
headers: {
'Content-Type': 'multipart/form-data'
}
}
)
.then(function (response) {
console.log(response);
})
.catch(function (error) {
console.log(error);
});
}
我也尝试过使用 CURL,效果很好。我的 CURL 命令是:
curl -X POST https://api.hubapi.com/oauth/v1/token -H "Content-Type: application/x-www-form-urlencoded" -d "grant_type=authorization_code" -d "client_id=yyyy" -d "client_secret=xxx" -d "redirect_uri=http://localhost:3000/api/hubspot/callback&scope=crm.schemas.deals.read%20oauth%20crm.objects.leads.read%20crm.objects.users.read%20crm.objects.contacts.write%20crm.objects.appointments.read%20crm.objects.companies.read%20crm.lists.read%20crm.objects.deals.read%20crm.schemas.contacts.read%20crm.objects.contacts.read%20crm.schemas.companies.read" -d "code=zzzz"
你能帮我解决我的node.js代码中缺少的内容吗?
我通过将标题更改为:
解决了这个问题 headers: {
'Content-Type': 'application/x-www-form-urlencoded',
charset: 'utf8',
accept: '*/*'
}