0

我想使用 Trustpilot API 发送电子邮件审核邀请。在拨打电话之前,我需要获得一个访问令牌。我在下面的函数中关注 Trustpilot 的文档。我不断收到 Unknown grant_type 这个错误。根据文档,它应该设置为“密码”以获取令牌并且它不起作用。我尝试了这个解决方案,但它对我不起作用。我似乎不知道是什么导致了错误,尤其是它非常普遍。

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
}
4

1 回答 1

1

请查看 axios文档。您正在传递content:而不是data:. axios 调用应该是这样的:

    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' },
        data: `grant_type=password&username=${process.env.TRUSTPILOT_EMAIL}&password=${process.env.TRUSTPILOT_PASSWORD}`,
    })
于 2021-09-24T15:22:41.737 回答