LinkedIn登录基本示例不起作用

Mas*_*iar 9 javascript linkedin-api

我想补充一下LinkedIn登录我项目的可能性.我按照LinkedIn开发者页面上给出的教程进行了操作,最后得到了以下代码,这些教程中非常基本的东西:

<!-- linkedin login -->
<script type="text/javascript" src="//platform.linkedin.com/in.js">
  api_key:   [MY_API_KEY] {~n}
  authorize: false {~n}
  onLoad: onLinkedInLoad {~n}
</script>

<!-- linkedin login play -->
<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>
Run Code Online (Sandbox Code Playgroud)

不要介意{~n}因为我正在使用dust.js来呈现页面,我需要它们将参数传递给LinkedIn脚本.

因此,出现登录按钮,这意味着LinkedIn能够正确连接到我的API密钥,但Chrome控制台给出了以下错误:Uncaught Error: Could not execute 'onLinkedInLoad'. Please provide a valid function for callback..由于这是教程页面上给出示例,我不知道它有什么问题.即使我在调用LinkedIn之前声明了LinkedIn登录功能块,我也会in.js得到同样的错误.

我认为发现这是一个非常容易的错误,但我无法确定我做错了什么.

Bha*_*ain 3

发生这种情况的原因是您在 head 部分中调用 onLinkedInLoad() ,并且如果您正在处理多个页面..即使它没有来自 API 调用的数据(或者只是您没有进行操作),它也会在每个页面上被调用出现此错误的页面上的任何调用)。

试试这个`

// Removing event listener by calling IN.Event.on() outside of 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);
}
Run Code Online (Sandbox Code Playgroud)

` 是的,从第一个脚本调用中删除“ onLoad: onLinkedInLoad {~n}”