【发布时间】:2021-11-17 00:08:34
【问题描述】:
我想使用 Trustpilot API 发送电子邮件审核邀请。在拨打电话之前,我需要获得一个访问令牌。我在下面的函数中关注了 Trustpilot 的 documentation。我不断收到 Unknown grant_type 这个错误。根据文档,它应该设置为“密码”以获取令牌并且它不起作用。我试过this solution,但它不适合我。我似乎不知道是什么导致了错误,尤其是它非常普遍。
trustPilot.getAuthToken = async () => {
let apiKey = process.env.TRUSTPILOT_API
let secrect = process.env.TRUSTPILOT_SECRET
let baseEncoded = Buffer.from(`${apiKey}:${secrect}`).toString('base64')
console.log(baseEncoded, 'base')
let authToken = null
try {
authToken = await axios({
method: 'POST',
url: `https://api.trustpilot.com/v1/oauth/oauth-business-users-for-applications/accesstoken`,
headers: { Authorization: 'Basic ' + baseEncoded, 'Content-Type': 'application/x-www-form-urlencoded' },
content: `grant_type=password&username=${process.env.TRUSTPILOT_EMAIL}&password=${process.env.TRUSTPILOT_PASSWORD}`,
})
console.log(authToken, 'auth')
} catch (error) {
console.log(error.response, 'err')
throw { code: '404' }
}
return authToken
}
【问题讨论】:
标签: javascript typescript axios request trustpilot