【问题标题】:Linkedin Integration with MeteorLinkedin 与 Meteor 的集成
【发布时间】:2015-09-27 22:28:29
【问题描述】:

我正在研究 Meteor,试图实现linkedin oauth。我有一个按钮,当用户单击它时,会出现一个窗口,要求用户允许访问,当用户允许时,必须显示用户的个人资料信息。

我的方法。

当按钮被点击时,我正在注入这个

"window.open('https://www.linkedin.com/uas/oauth2/authorization?response_type=code&client_id=XXXXXXX&redirect_uri=http%3A%2F%2Flocalhost%3A4000%2F_oauth%2Flinkedin%3Fclose&scope=&state=XXXXX', 'newwindow', 'width=400, height=250');"

打开一个询问访问权限的新窗口。当用户通过提供用户名和密码允许访问时,窗口会立即关闭。我知道linkedin将它定向到我们的应用程序,并在url中提供授权代码和状态。我需要使用这个授权码来获取访问令牌。但我无法捕捉到这个授权码。

请告诉我如何实现此功能以及我的方法是否正确。

【问题讨论】:

    标签: javascript meteor oauth-2.0 linkedin linkedin-api


    【解决方案1】:

    使用 accounts-linkedin 包执行此操作:https://atmospherejs.com/pauli/accounts-linkedin

    您可以添加它: meteor add accounts-linkedin

    或者,如果您真的想知道它是如何完成的,请查看该包的源代码。

    【讨论】:

    • 感谢您的 -1。哈哈。很高兴来到这里并回答您的问题以获得否决票。过去它对我有用。并且有 8.3k 的下载量,所以问题可能出在您身上
    • 我没有投票给你,你这个傻瓜。我刚刚创建了我的 SO 帐户并且只有 2 个声誉,所以不能对任何问题投反对票。
    • 哈哈。我的错。有两个人投了你的票!不过不是我。最诚挚的歉意
    • 不用担心...可能他们没有理解这个问题,f!tards!
    【解决方案2】:

    这是对所有投反对票的人的答复。我终于可以做到了。

    Session.set('authCode',null);
    Session.set('profile',null);
    Template.home.events({
        'click .viewLinkedinProfileButton' :function(event, template){
            var redirect_uri = encodeURIComponent(Meteor.absoluteUrl('userAuthComplete'));
            var client_id='XXXXXXX';
            var state = 'myRandomString';
            var scope = ['r_basicprofile'];
            var url = "https://www.linkedin.com/uas/oauth2/authorization?response_type=code&client_id="+client_id+"&redirect_uri="+redirect_uri+"&state="+state+"&scope=r_basicprofile";
    
            showPopup(url,function(err){
                if(err){
                    console.log("Cancelled "+err);
                }
                else{
                    Meteor.call('getAccessToken',Session.get('authCode'),function(err,res){
                        if(res){
                            console.log(res);
                            Session.set('profile',res);
                        }
                    });
                    Session.set('authCode',null);
                }
            })
        }
    });
    
    function showPopup(url, callback, dimensions) {
        // default dimensions that worked well for facebook and google
        var popup = openCenteredPopup(
            url,
            (dimensions && dimensions.width) || 650,
            (dimensions && dimensions.height) || 331
        );
    
        var checkPopupOpen = setInterval(function() {
            try {
                var popupClosed = popup.closed || popup.closed === undefined;
            } catch (e) {
                return;
            }
    
            if (popupClosed) {
                var url = popup.document.URL;
                if(url.toLowerCase().indexOf('code') !== -1){
                    var array1 = url.split('code=');
                    var array2 =array1[1].split('&');
                    Session.set('authCode',array2[0]);
                    clearInterval(checkPopupOpen);
                    callback();
                }
                else{
                    clearInterval(checkPopupOpen);
                }
            }
        }, 50);
    }
    
    function openCenteredPopup(url, width, height) {
        var screenX = typeof window.screenX !== 'undefined'
            ? window.screenX : window.screenLeft;
        var screenY = typeof window.screenY !== 'undefined'
            ? window.screenY : window.screenTop;
        var outerWidth = typeof window.outerWidth !== 'undefined'
            ? window.outerWidth : document.body.clientWidth;
        var outerHeight = typeof window.outerHeight !== 'undefined'
            ? window.outerHeight : (document.body.clientHeight - 22);
        var left = screenX + (outerWidth - width) / 2;
        var top = screenY + (outerHeight - height) / 2;
        var features = ('width=' + width + ',height=' + height +
        ',left=' + left + ',top=' + top + ',scrollbars=yes');
    
        var newwindow = window.open(url, 'Login', features);
        if (newwindow.focus)
            newwindow.focus();
        return newwindow;
    }
    
    
    Template.home.helpers({
       profile:function(){
           return Session.get('profile');
       }
    });
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-10-11
      • 1970-01-01
      • 1970-01-01
      • 2013-09-16
      相关资源
      最近更新 更多