salesforce-marketingcloud / FuelSDK-Node-REST

Node REST client w/ auth to access the Salesforce Marketing Cloud (formerly ExactTarget) API
BSD 3-Clause "New" or "Revised" License
43 stars 22 forks source link

Authentication error while making request #104

Closed akurudi closed 4 years ago

akurudi commented 4 years ago

I'm trying to make a simple GET request using OAuth 2.0 and grant_type: 'client_credentials' but i keep getting an error. I followed the readme doc to initialize and make a request like below.

const FuelRest = require("fuel-rest");

const options = {
  auth: {
    clientId: "CLIENT_ID_HERE",
    clientSecret: "CLIENT_SECRET_HERE",
    authUrl:
      "https://MY_AUTH_URL.auth.marketingcloudapis.com/v2/token/", //Tried without '/v2/token/' also
  },
  origin: "https://MY_ORIGIN.rest.marketingcloudapis.com/",
};

const RestClient = new FuelRest(options);

const getOpts = {
  uri: "/platform/v1/endpoints",
  headers: {},
  // other request options
};

RestClient.get(getOpts)
  .then(function (response) {
    var result = response && response.body ? response.body : response;
    console.log(result);
  })
  .catch(function (err) {
    console.log(err);
  });

Error i am getting is

{ Error: No access token
    at AuthClient.getAccessToken.then.tokenInfo (E:\Development\node-scripts\node_modules\fuel-rest\lib\fuel-rest.js:88:18)
    at <anonymous>
    at process._tickCallback (internal/process/next_tick.js:189:7)
  res:
   { error: 'unsupported_grant_type',
     error_description: 'Use "authorization_code" or "refresh_token" or "client_credentials" or "urn:ietf:params:oauth:grant-type:jwt-bearer" as the grant_type.',
     error_uri: 'https://developer.salesforce.com/docs' } }

Please let me know if i did something wrong with the initialization or misunderstood the docs.

akurudi commented 4 years ago

If someone else stumbles across this, I was missing authOptions in the rest client initialization. Below is the correct way to init the rest client.

const options = {
  auth: {
    clientId: "CLIENT_ID_HERE",
    clientSecret: "CLIENT_SECRET_HERE",
    authUrl:
      "https://MY_AUTH_URL.auth.marketingcloudapis.com/v2/token/", 
authOptions: {
      authVersion: 2,
    }
  },
  origin: "https://MY_ORIGIN.rest.marketingcloudapis.com/",
};
const RestClient = new FuelRest(options);