避免在HTML中暴露LinkedIn API密钥

时间:2018-02-25 05:00:41

标签: javascript node.js express oauth-2.0 linkedin-api

我希望我的用户使用使用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的情况下实现此目的?那里有几个npm包来处理这种事情,但它们都已经过时了(每当一个软件包至少在一年内没有更新时我会感到紧张)。

我的应用程序使用node和express。我应该使用旧的npm软件包还是有更好的方法来隐藏api_key?

1 个答案:

答案 0 :(得分:1)

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

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

API Key&amp;密钥

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

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

由于需要同时使用API​​密钥和密钥来确认您的应用程序的身份,因此永远不要暴露您的密钥至关重要。以下是正确密钥存储的一些建议:

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

不要在任何客户端公开JavaScript或HTML文件等代码文件。

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

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

请勿通过电子邮件发送或将其发布在留言板或其他公共论坛上。

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