2015-07-11 64 views
0

我被困在某个时刻。我想打到linkedin API(不使用任何软件包)并获取用户的配置文件信息。使用流星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的用户名和密码。但是当我提供凭据并按“允许访问”时,我在浏览器控制台中收到此错误。

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

以及在服务器上,我得到这个 无法从OAuth的查询解析状态:由于错误说没有标头叫'Access-Control-Allow-Origin',尝试添加DC8ÄDF

回答

0

好,

钍是非常令人费解的。因为我的代码没有错误。但最后我发现问题出在linkedin应用程序。我在6个月前创建的应用程序使用了上述代码。但是,当我尝试使用新创建的应用程序。

1

头像这样:

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

尝试,让我们知道,如果它的工作原理

+0

嗨,我试过..仍然是给出相同的解决方案。我真的很想知道问题是什么。任何想法,授权代码持续多久?我在某个地方读了20多年,但我没有在linkedin文档中看到它。是否因为我的授权码过期?但是我在创建它的那一刻起就调用它 – Juvenik

+0

在修复头文件后,您会收到完全相同的错误消息?我认为认证码不是问题。 – FullStack

+0

是的,我在控制台中得到相同的错误。你可以在你的系统中尝试上面的代码,并在控制台中看到它。 – Juvenik