这可能吗?我正在做一个前端导师挑战,在搜索用户后连接并提取 Github 用户信息。一切正常并已连接。我已准备好推送我的代码并提交我的项目,但我想隐藏我的 API key 。我是 JS 的新手,有没有办法在没有 NodeJS 的情况下做到这一点?我自己删除了 key ,但这是我获取 API 数据的地方。
class Github {
constructor() {
this.client_id = CLIENT_ID_GOES_HERE;
this.client_secret = CLIENT_SECRET_GOES_HERE;
}
async getUser(user) {
const profileResponse = await fetch(`https://api.github.com/users/${user}?client_id=${this.client_id}&client_secret=${this.client_secret}`);
const profileData = await profileResponse.json();
console.log(profileData);
return {
profileData,
};
}
}
最佳答案
不可能,客户端使用的任何 javascript key 都必须是公开的,您应该仅在服务器端使用 Github secret ,使用 node-js 和环境变量以避免将其添加到公共(public)存储库。
https://medium.com/chingu/an-introduction-to-environment-variables-and-how-to-use-them-f602f66d15fa
关于javascript - 如何使用 Vanilla JS 隐藏 API key ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/71656252/