【问题标题】:Cannot authorize with LinkedIn无法通过 LinkedIn 授权
【发布时间】:2016-02-04 05:20:06
【问题描述】:

我正在尝试从我的 LinkedIn 网站上的用户那里获取 名字、姓氏和电子邮件。这就是我所做的:

在我的 LinkedIn 应用中,我将 默认范围(OAuth 用户协议)设置为:

  • r_basicprofile
  • r_contactinfo
  • w_share
  • r_emailaddress

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

我还复制了我的 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>

【问题讨论】:

  • @JeremyThille:我已经为我的主题添加了更新。你能看一下吗?
  • @JeremyThille,那他们为什么要把它放在入门主题中??
  • Jeremy - 文档是正确的,您也正确,这显然不是标准的 Javascript 表示法。

标签: javascript jquery oauth oauth-2.0 linkedin


【解决方案1】:

您似乎只是遇到了库的异步性问题。我已经为您稍微修改了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>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-05-14
    • 2014-03-03
    • 1970-01-01
    • 1970-01-01
    • 2014-01-31
    • 2012-12-24
    相关资源
    最近更新 更多