[英]Get LinkedIn Access Token with OAuthSimple in Javascript
I'm using OAuthSimple in Javascript with PIN based authentication (OOB flow). 我正在Javascript中使用OAuthSimple进行基于PIN的身份验证 (OOB流)。 We are developing an HTML5 app which lives inside a mobile device's native wrapper using PhoneGap. 我们正在开发一个HTML5应用程序,该应用程序可以使用PhoneGap驻留在移动设备的本地包装中。 There's not ANY server side (no URL at all), all requests are sent using the mobile device as a proxy. 没有任何服务器端(根本没有URL),所有请求都使用移动设备作为代理发送。
So far I managed to: 到目前为止,我设法:
- Get Request Token -获取请求令牌
- Redirect user to authorization page -将用户重定向到授权页面
- Got authorization PIN -获得授权PIN
I need sample code that shows how to get an Access Token using OAuthSimple Javascript library . 我需要示例代码,展示如何使用OAuthSimple Javascript库获取访问令牌 。
Any help will be appreciated. 任何帮助将不胜感激。 Thanks! 谢谢!
Not sure if you are the same person who posted on our forums recently (see post here https://developer.linkedin.com/forum/oauthsimple-request-access-token ), but I replied with demo code on how to do just that using OAuthSimple. 不确定您是否与最近在我们论坛上发布的人相同(请参阅此处https://developer.linkedin.com/forum/oauthsimple-request-access-token ),但是我回答了演示代码使用OAuthSimple。
The actual sample code can be found here: https://gist.github.com/efc88a38da25ff4e9283 实际的示例代码可以在这里找到: https : //gist.github.com/efc88a38da25ff4e9283
If you need any help using it, don't hesitate to reach out! 如果您需要使用它的任何帮助,请随时与我们联系!
-Jeremy -Jeremy
This will create a phonegap linkedIn login, its just a worked around code though, but this works atleast for me 这将创建一个phonegap链接登录,虽然它只是一个可解决的代码,但这对我来说至少是有效的
var consumer_key = "key";
var shared_secret = "secrete";
self.oauth = OAuthSimple(consumer_key, shared_secret); var linkedInScope = 'r_basicprofile r_emailaddress w_messages r_network';
var url = self.oauth.sign({action: "GET", path: "https://api.linkedin.com/uas/oauth/requestToken", parameters: {scope: linkedInScope, oauth_callback: "oob"}}).signed_url;
var request = 'requestToken';
var linkedInObj = new Object;
function linkedInWorkAround(url,request,data){
var callType = 'GET';
var xhr = $.ajax({
url : url,
beforeSend : function(){
$.mobile.loading( 'show' );
if(request == 'linkedIn_login'){
callType = 'POST';
}
},
timeout : 8000,
data : data,
type : callType,
success: function(r){
$.mobile.loading( 'hide' );
if(request == 'requestToken'){
var oauthRes = r.split('&');
$.each(oauthRes, function(k,v){
var resObj = v.split('=')
linkedInObj[resObj[0]] = resObj[1];
});
url = 'https://www.linkedin.com/uas/oauth/authenticate?scope='+linkedInScope+'&oauth_token='+linkedInObj.oauth_token;
request = 'oauth_token';
linkedInWorkAround(url,request);
}
else if(request == 'oauth_token'){
var accessCode = $(r).find('.access-code');
if(accessCode.size()){
self.oauth.reset();
var pin = $(r).find('.access-code').text();
url = self.oauth.sign({action: "GET", path: "https://api.linkedin.com/uas/oauth/accessToken", parameters: {scope: linkedInScope, oauth_verifier: pin}, signatures: linkedInObj}).signed_url;
request = 'accessToken';
linkedInWorkAround(url,request);
}
else{
$('.custom-linkedIn').remove();
var cloneIn = $(r).find('form').addClass('custom-linkedIn').clone();
$('a,span,select,.duration-label,.access',cloneIn).hide();
$('#pageLinkedIn .errMsgHolder').after(cloneIn)
$('#session_key-oauthAuthorizeForm').textinput();
$('#session_password-oauthAuthorizeForm').textinput();
$('input[type=submit]').button();
$('form.custom-linkedIn').submit(function(){
$('.errMsgHolder').hide().text('');
url = 'https://www.linkedin.com/uas/oauth/authorize/submit';
request = 'linkedIn_login';
var data = $(this).serialize();
linkedInWorkAround(url,request,data);
return false;
});
}
}
else if(request == 'linkedIn_login'){
self.oauth.reset();
var pin = $(r).find('.access-code').text();
url = self.oauth.sign({action: "GET", path: "https://api.linkedin.com/uas/oauth/accessToken", parameters: {scope: linkedInScope, oauth_verifier: pin}, signatures: linkedInObj}).signed_url;
request = 'accessToken';
linkedInWorkAround(url,request);
}
else if(request == 'accessToken'){
var oauthRes = r.split('&');
self.oauth.reset();
$.each(oauthRes, function(k,v){
var resObj = v.split('=')
linkedInObj[resObj[0]] = resObj[1];
});
url = self.oauth.sign({action: "GET", path: "https://api.linkedin.com/v1/people/~/email-address", signatures: linkedInObj}).signed_url;
request = 'getResultLinkedIn';
linkedInWorkAround(url,request);
}
else if(request == 'getResultLinkedIn'){
$('body').css({opacity:0});
var userLIemail = ($('#session_key-oauthAuthorizeForm').size()) ? $('#session_key-oauthAuthorizeForm').val() : $(r).text();
}
},
error : function(a,b,c){
alert('err')
console.log(a,b,c)
self._cs(a)
$.mobile.loading( 'hide' );
if(a.statusText.toLowerCase() == 'unauthorized'){
$('.errMsgHolder').show().text('The email address or password you provided does not match our records');
}
}
})
}
linkedInWorkAround(url,request);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.