Search code examples
javascriptmeteoroauth-2.0linkedin-api

Linkedin Integration with Meteor


I am working on Meteor trying to implement linkedin oauth. I have a button, when user clicks it, a window appears asking the user to allow access, when the user allows, the profile info of the user has to be displayed.

My Approach.

When button is clicked, I am injecting this

"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');"

which opens a new window that asks access permission. And when user allows access by giving username and password, the window goes off instantly. I know that linkedin directs it to our app giving authorization code and state in the url. And I need to use this authorization code to get the access token. But I am not being able to catch this authorization code.

Please let me know how can I achieve this functionality and if my approach is correct.


Solution

  • This is a reply for all those who voted down the question. I was finally able to do it.

    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');
       }
    });