javascript - Hitting Linkedin API using Meteor JS -
i stuck @ point. want hit linkedin api (without using package) , profile information of user.
so this, following these steps: 1. hit linkedin api authorization code 2. use auth code access token. 3. using access token profile info.
so far, being able authorization code, not being able proceed further. here codes:
these testing purpose doing on client.
my template
<template name="home"> <a href="#" class="calllinkedin">get profile 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 pop linkedin username , password. when give credentials , press "allow access", getting 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 present on requested resource. origin 'http:// localhost:4000' therefore not allowed access. response had http status code 400.
and in server, got unable parse state oauth query: dc8Ädf
since error says there no header called 'access-control-allow-origin'
, try adding header so:
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); } });
try , let know if works
Comments
Post a Comment