英文:
how to use keap api , making OAuth request without user authorization?
问题
如何在Node.js应用程序中使用Keap API?文档不够详细,我想要实现我提供的文章,我尝试了ChatGPT生成的代码,但总是出现错误提示:
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";
// 步骤1:获取刷新令牌
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;
};
// 步骤2:使用刷新令牌获取访问令牌
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;
};
// 依次调用这些函数
(async () => {
try {
const refreshToken = await getRefreshToken();
const accessToken = await getAccessToken(refreshToken);
} catch (error) {
console.error(error);
}
})();
英文:
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);
}
})();
答案1
得分: 1
将client_id
和客户端密钥进行编码,并放置在Header:Authorization
中。
参考链接:https://developer.infusionsoft.com/getting-started-oauth-keys/
英文:
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/
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论