如何在 Nodejs 应用程序中使用 Keap api?它没有很好的记录,我想实现我提供的文章,我尝试了由 chatGPT 生成的代码,但总是出现错误:
错误:'unauthorized_client',error_description:'client_id 不 拥有受信任的服务帐户角色'
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);
}
})();
您需要对
client_id
和客户端密钥进行编码并将其放入 Header:Authorization
参考:https://developer.infusionsoft.com/getting-started-oauth-keys/