无法通过LinkedIn授权

时间:2015-03-02 08:21:58

标签: javascript jquery oauth oauth-2.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未定义。

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

更新

我指定API密钥的javascript代码,...我已经从"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>

1 个答案:

答案 0 :(得分:2)

您可能只是遇到了图书馆的异步性问题。我已经为您稍微修改了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>
相关问题