Reem Obeid
Reem Obeid

Reputation: 85

Trustpilot Authentication Error Unknown grant_type

I want to use Trustpilot API to send email review invitation. Before making that call, I need to get an access token. I'm following Trustpilot's documentation in the function below. I keep getting this error of Unknown grant_type. According to the documentation, it is supposed to be set to "password" to get the token and it is not working. I tried this solution but it is not working for me. I can't seem to know what's causing the error especially that it is very general.

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
}

Upvotes: 0

Views: 999

Answers (1)

Arunas Stonis
Arunas Stonis

Reputation: 179

Please take a look at axios documentation. You are passing content: instead of data:. Axios call should be like this:

    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}`,
    })

Upvotes: 2

Related Questions