【问题标题】:Trustpilot Authentication Error Unknown grant_typeTrustpilot 身份验证错误未知 grant_type
【发布时间】: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


    【解决方案1】:

    请查看 axios documentation。您传递的是 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}`,
        })
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-01-10
      • 1970-01-01
      • 1970-01-01
      • 2015-04-03
      • 2022-01-11
      • 2014-02-22
      相关资源
      最近更新 更多