Search code examples
api

how to use keap api , making OAuth request without user authorization?


how can I use Keap api in a nodejs app? it's not well documented and I want to implement the article I provided , I tried this code generated by chatGPT but there's always an error saying:

error: 'unauthorized_client', error_description: 'client_id does not have a trusted service account role'

const CLIENT_ID = "clientId";
const CLIENT_SECRET = "clientSecret";
const GRANT_TYPE_AUTH = "client_credentials";
const GRANT_TYPE_TOKEN = "refresh_token";
const SCOPE = "full";

// Step 1: Obtain a refresh token
const getRefreshToken = async () => {
  const url = "https://api.infusionsoft.com/token";
  const data = {
    client_id: CLIENT_ID,
    client_secret: CLIENT_SECRET,
    grant_type: GRANT_TYPE_AUTH,
    scope: SCOPE,
  };
  const config = {
    headers: { "Content-Type": "application/x-www-form-urlencoded" },
  };
  const response = await axios.post(url, qs.stringify(data), config);
  console.log(response);
  return response.data.refresh_token;
};

// Step 2: Obtain an access token using the refresh token
const getAccessToken = async (refreshToken) => {
  const url = "https://api.infusionsoft.com/token";
  const data = {
    client_id: CLIENT_ID,
    client_secret: CLIENT_SECRET,
    grant_type: GRANT_TYPE_TOKEN,
    refresh_token: refreshToken,
  };
  const config = {
    headers: { "Content-Type": "application/x-www-form-urlencoded" },
  };
  const response = await axios.post(url, qs.stringify(data), config);
  return response.data.access_token;
};

// Call the functions in order
(async () => {
  try {
    const refreshToken = await getRefreshToken();
    const accessToken = await getAccessToken(refreshToken);
  } catch (error) {
    console.error(error);
  }
})();


Solution

  • You need to encode the client_id and client secret and place it in Header:Authorization

    Reference: https://developer.infusionsoft.com/getting-started-oauth-keys/