避免在 HTML 中暴露 LinkedIn API 键

Avoid exposing LinkedIn API key in HTML

我希望我的用户使用使用 LinkedIn 功能登录。 LinkedIn API 文档提供了以下入门示例片段:

<script type="text/javascript" src="//platform.linkedin.com/in.js">
    api_key: YOUR_API_KEY_HERE
    authorize: true
    onLoad: onLinkedInLoad
</script>

<script type="text/javascript">

    // Setup an event listener to make an API call once auth is complete
    function onLinkedInLoad() {
        IN.Event.on(IN, "auth", getProfileData);
    }

    // Handle the successful return from the API call
    function onSuccess(data) {
        console.log(data);
    }

    // Handle an error response from the API call
    function onError(error) {
        console.log(error);
    }

    // Use the API call wrapper to request the member's basic profile data
    function getProfileData() {
        IN.API.Raw("/people/~").result(onSuccess).error(onError);
    }

</script>

如何在不将 YOUR_API_KEY_HERE 暴露给 public 的情况下实现它?有一些 npm 包可以处理这种事情,但它们都已经过时了(每当一个包至少一年没有更新时,我都会感到紧张)。

我的应用程序使用节点和快递。我应该使用旧的 npm 包还是有更好的方法来隐藏 api_key?

在 javascript 或网站中使用 YOUR_API_KEY_HERE 是可以的,也是必要的,有时是必要的。他们重要的一点是不要分享你的 SECRET_KEY,因为你需要两者都对 API 做任何事情。请务必始终使用 HTTPS 进行所有通信。

来自安全应用网站的 linkedin 最佳实践: https://developer.linkedin.com/docs/best-practices

API 密钥和密钥

调用 LinkedIn API 时,您使用两条可识别信息:API 密钥(有时称为消费者密钥)和秘密密钥(或消费者秘密)。

API 密钥是您应用程序的 public 标识符,密钥是机密的,只能用于在 LinkedIn API 上验证您的应用程序。

由于需要同时使用 API 密钥和密钥来确认您的应用程序的身份,因此切勿泄露您的密钥。以下是正确存储密钥的一些建议:

创建本机移动应用程序时,不要将其本地存储在移动设备上。

不要在任何客户端代码文件中公开,例如 JavaScript 或 HTML 文件。

不要将其存储在可以从外部查看的 Web 服务器上的文件中,例如:配置文件、包含文件等。

不要将其存储在日志文件或错误消息中。

不要通过电子邮件或post留言板或其他public论坛。

请记住,在为访问令牌交换 OAuth 2.0 授权代码时,密钥作为请求的一部分传递。不要公开此请求 publicly!