2015-03-02 119 views
0

我正尝试使用LinkedIn在我的网站上为用户提供的名字和电子邮件地址。这是我做了什么:无法使用LinkedIn授权

在我的LinkedIn应用程序,我设置的默认范围(OAuth的用户协议)来:

  • r_basicprofile
  • r_contactinfo
  • w_share
  • r_emailaddress

我已将我的网域正确添加到JavaScript API域。我没有添加到OAuth 2.0重定向网址的链接。但我不知道这是否是强制性的(以及要插入哪条路径)?

我也复制我的API密钥(消费者密钥)。

现在在我的HTML我有:

<script type="text/javascript" src="http://platform.linkedin.com/in.js"> 
    lang: en_US 
    api_key: myapikey 
    scope: r_basicprofile r_emailaddress 
</script> 

<input class="apply-with-linkedin" type="button" value="Apply with LinkedIn" id="btn-linkedin-apply"> 

<script type="text/javascript"> 
    jQuery('#btn-linkedin-apply').click(function (e) { 
     e.preventDefault(); 
     e.stopPropagation(); 

     IN.User.authorize(function() 
     { 
      IN.API.Profile('me').fields([ 
       'firstName', 
       'lastName', 
       'emailAddress' 
      ]).result(function (profiles) 
      { 
       var me = profiles.values[0]; 

       if (me.hasOwnProperty('firstName')) { 
        jQuery('#apply-form #input-firstname').val(me.firstName); 
       } 

       if (me.hasOwnProperty('lastName')) { 
        jQuery('#apply-form #input-lastname').val(me.lastName); 
       } 

       if (me.hasOwnProperty('emailAddress')) { 
        jQuery('#apply-form #input-email').val(me.emailAddress); 
       } 
      }); 
     }); 
    }); 
</script> 

但是,当我按一下按钮我总是得到JavaScript错误Cannot read property 'authorize' of undefinedIN.User未定义。

这可能是什么问题? ...

UPDATE:

的JavaScript代码,我指定我的API密钥,......我从"Getting Started with the JavaScript SDK" from LinkedIn复制。

<script type="text/javascript" src="//platform.linkedin.com/in.js"> 
    api_key: [API_KEY] 
    onLoad: [ONLOAD] 
    authorize: [AUTHORIZE] 
    lang:  [LANG_LOCALE] 
</script> 
+0

'郎咸平:EN_US API_KEY:myapikey范围:r_basicprofile r_emailaddress'是远不有效的JavaScript。 – 2015-03-02 08:25:14

+0

@JeremyThille:我为我的主题添加了更新。你可以看一下吗? – nielsv 2015-03-02 08:31:43

+0

是的,这就是我说的:这绝对不是Javascript。在我看来,你甚至没有这门语言的基础知识。这些行不能运行,并且显然会导致错误。尝试先写一些Javascript。 – 2015-03-02 08:33:42

回答

3

您似乎只是遇到了库的异步性问题。我已经稍微修改了Sign in with LinkedIn Javascript示例中的示例代码,但我认为您的问题将通过更多关注回调来解决,以便您知道a)库已加载,以及b)API调用已成功完成 - 试图访问所产生的任何数据之前:

<script type="text/javascript" src="//platform.linkedin.com/in.js"> 
    api_key: YOUR_API_KEY_HERE 
    authorize: true 
    scope: r_basicprofile r_emailaddress 
    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) { 
     // Pre-populate your form fields here once you know the call 
     // came back successfully 
    } 

    // 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/~:(firstName,lastName,emailAddress)").result(onSuccess).error(onError); 
    } 

</script>