我想添加LinkedIn登录我项目的可能性。我按照LinkedIn developer page上给出的教程进行了操作,最后得到了以下代码,这个教程非常基本:
<!-- 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>
不要介意{~n}
,因为我正在使用dust.js来呈现页面,我需要它们将参数传递给LinkedIn脚本。
因此,出现登录按钮,这意味着LinkedIn能够正确连接到我的API密钥,但Chrome控制台出现以下错误:Uncaught Error: Could not execute 'onLinkedInLoad'. Please provide a valid function for callback.
。由于这是教程页面上给出的 示例,我不知道它有什么问题。即使我在调用LinkedIn in.js
之前声明了LinkedIn登录功能块,我也会遇到同样的错误。
我认为发现这是一个非常容易犯的错误,但我无法确定我做错了什么。
答案 0 :(得分: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);
}
` 是的删除&#34; onLoad:onLinkedInLoad {~n}&#34;从第一个脚本调用