I am stuck at some point. I want to hit the linkedin API (WITHOUT using any package) and get the profile information of the user.
So for this, I am following these steps: 1. Hit the linkedin API to get authorization code 2. Use this auth code to get the access token. 3. Using access token to get the profile info.
So far, I am being able to get the authorization code, but not being able to proceed further. Here are my codes:
These are just for testing purpose so doing everything on the client.
My template
<template name="home">
<a href="#" class="callLinkedIn">get profile from linkedin</a>
</template>
My helpers
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;
}
I get the pop up with linkedin username and password. But when I give the credentials and press "allow access", I am getting this error in browser console.
POST https://www.linkedin.com/uas/oauth2/accessToken XMLHttpRequest cannot load https://www.linkedin.com/uas/oauth2/accessToken. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http:// localhost:4000' is therefore not allowed access. The response had HTTP status code 400.
And also in the server, I got this Unable to parse state from OAuth query: DC8ÄDF