Search code examples
javascriptnode.jsmeteortrustpilot

Unknow grant_type status code 401 TrustPilot email invitation authentification


I am trying to use TrustPilot API to send email invitation for reviews. No matter what i do the result is unknown grant_type. The documentation says that the grant_type should be set to "password" in order to get the token so i can use to send the invitation with the proper options.

export const inviteBuyer = async (email, orderNumber) => {
  const base64 = new Base64();
  const secrets = base64.encode('APIKEY:APISECRET');
  const authOptions = {
    data: 'grant_type=password&username=myemail&password=mypassword',
    headers: {
      Authorization: 'Basic ' + secrets,
      'Content-Type': 'application/x-www-form-urlencoded',
    },
  };

  const authUrl = 'https://api.trustpilot.com/v1/oauth/oauth-business-users-for-applications/accesstoken';

  try {
    const getIdentity = await HTTP.call('POST', authUrl, authOptions);
  } catch (err) {
    console.log(err);
  }

  const options = {
    headers: {
      token: getIdentity.access_token,
    },
    parameters: {
      consumerEmail: email,
      consumerName: 'John Doe',
      referenceNumber: orderNumber,
      locale: 'en',
      senderEmail: 'OurEMail', // potentiellmeent
      serviceReviewInvitation: {
        // preferredSendTime: '2013-09-07T13:37:00',
        // redirectUri: 'http://trustpilot.com',
        tags: ['buyer'],
        // templateId: '507f191e810c19729de860ea',
      },
    },
  };

  try {
    const resp = await HTTP.call(
      'POST',
      'https://invitations-api.trustpilot.com/v1/private/business-units/OurBusinessUnitId/email-invitations',
      options
    );
    console.dir(resp);
  } catch (err) {}
  console.log(err);
};

Solution

  • The original question used a payload key for specifying the body, and later on data.

    However, to send a raw HTTP request body with the Meteor HTTP client, content must be used:

      const authOptions = {
        content: 'grant_type=password&username=myemail&password=mypassword',
        headers: {
          Authorization: 'Basic ' + secrets,
          'Content-Type': 'application/x-www-form-urlencoded',
        },
      };