-2

我正在研究 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中提供授权代码和状态。我需要使用这个授权码来获取访问令牌。但我无法捕捉到这个授权码。

请让我知道如何实现此功能以及我的方法是否正确。

4

2 回答 2

0

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

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

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

于 2015-07-09T14:31:10.327 回答
0

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

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');
   }
});
于 2015-07-15T05:19:44.653 回答