使用 Meteor JS 访问 Linkedin API

Hitting Linkedin API using Meteor JS

我卡在了某个时候。我想点击 linkedin API(不使用任何包)并获取用户的个人资料信息。

为此,我将执行以下步骤: 1.点击linkedin API获取授权码 2. 使用此授权码获取访问令牌。 3. 使用访问令牌获取配置文件信息。

到目前为止,我可以获取授权码,但无法进行下一步。这是我的代码:

这些仅用于测试目的,因此请在客户端执行所有操作。

我的模板

<template name="home">
    <a href="#" class="callLinkedIn">get profile from linkedin</a> 
</template>

我的帮手

var clientId='XXXXX';
var secret = 'XXXXX';
var redirectUri = 'http%3A%2F%2Flocalhost%3A4000%2F_oauth%2Flinkedin%3Fclose';
var credentialToken = "RANDOMSTRING";


Template.home.events({
    'click .callLinkedIn':function(event,template){
        var scope = [];
        var loginUrl =
            'https://www.linkedin.com/uas/oauth2/authorization' +
            '?response_type=code' + '&client_id=' + clientId +
            '&redirect_uri=' + redirectUri +
            '&scope=' + scope + '&state=' + credentialToken;

        showPopup(loginUrl,function(err){
            if(err){
                console.log(err);
            }
            else{
                console.log('success');
                var params = {
                    'grant_type':'authorization_code',
                    'code':Session.get('code'),
                    'redirect_uri':redirectUri,
                    'client_id':clientId,
                    'client_secret':secret
                };

                HTTP.call('POST',"https://www.linkedin.com/uas/oauth2/accessToken", {
                        headers:{'Content-Type':'application/x-www-form-urlencoded'},
                        params:params
                    },
                    function(err,res){
                        if(err){
                            console.log(err);
                        }
                        else{
                            console.log(res);
                        }
                    });
            }
        })

    }
})

function showPopup(url, callback, dimensions) {                                                      
    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) {
            console.log(popup.document.URL);
            var url = popup.document.URL;
            var a1 = url.split('code=');
            var a2 =a1[1].split('&');
            Session.set('code',a2[0]);
            clearInterval(checkPopupOpen);                                              
            callback();                                                                 
        }                                                                               
    }, 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;                                                    
}

我收到带有 linkedin 用户名和密码的弹出窗口。但是当我提供凭据并按 "allow access" 时,我在浏览器控制台中收到此错误。

POST https://www.linkedin.com/uas/oauth2/accessToken XMLHttpRequest 无法加载 https://www.linkedin.com/uas/oauth2/accessToken。请求的资源上不存在 'Access-Control-Allow-Origin' header。因此不允许 Origin 'http:// localhost:4000' 访问。响应具有 HTTP 状态代码 400。

而且在服务器中,我得到了这个 无法从 OAuth 查询解析状态:DC8ÄDF

由于错误表明没有 header 调用 'Access-Control-Allow-Origin',请尝试像这样添加 header:

HTTP.call('POST',
  "https://www.linkedin.com/uas/oauth2/accessToken", {
    headers : {
      'Content-Type':'application/x-www-form-urlencoded', 
      'Access-Control-Allow-Origin' : '*'  
    },
    params : params
  },
  function(err,res){
    if(err){
      console.log(err);
    }
    else{
      console.log(res);
    }
  });

尝试一下,如果有效请告诉我们

嗯,

这很令人费解。因为我的代码没有错误。但最后我发现问题出在 linkedin 应用程序上。我将上述代码用于大约 6 个月前创建的应用程序。但是当我尝试使用新创建的应用程序时它起作用了。