如何使用Keap API,进行OAuth请求而无需用户授权?

huangapple go评论125阅读模式
英文:

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/

huangapple
  • 本文由 发表于 2023年3月9日 14:18:31
  • 转载请务必保留本文链接:https://go.coder-hub.com/75681029.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定