javascript - 如何使用 Vanilla JS 隐藏 API key

标签 javascript github-api api-key

这可能吗?我正在做一个前端导师挑战,在搜索用户后连接并提取 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/

相关文章:

bash - 如何在 shell 脚本中使用个人访问 token 访问 GitHub API?

javascript - 推送到数组的值不会保留

javascript - 在 chrome 中的弹出窗口和内容脚本之间进行通信

javascript - 如何修复脚本?

c# 类到 Js 类?

github - 如何从 Github API V3 上的发布或标签获取提交 SHA

GitHub API v3 获取用户的所有问题

meteor - git 忽略和不提交但仍然使用全局变量的概念

api - 我在哪里可以找到我的 Yahoo Developer API key ?

java - 通过简单的 API key 使用 Google Cloud Vision API