【发布时间】:2015-07-11 03:45:58
【问题描述】:
我被困在某个时刻。我想点击linkedin API(不使用任何包)并获取用户的个人资料信息。
为此,我遵循以下步骤: 1.点击linkedin API获取授权码 2. 使用此验证码获取访问令牌。 3. 使用access token获取profile信息。
到目前为止,我能够获得授权码,但无法继续进行。这是我的代码:
这些只是为了测试目的,所以在客户端上做所有事情。
我的模板
<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用户名和密码的弹出窗口。但是当我提供凭据并按“允许访问”时,我在浏览器控制台中收到此错误。
发布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 查询的状态:DC8ÄDF
【问题讨论】:
标签: javascript meteor oauth-2.0 linkedin