2011-12-31 71 views
2

我在使用OAuthSimple使用Javascript与基于PIN的身份验证(OOB流)。 我们正在开发一个HTML5应用程序,它使用PhoneGap存在于移动设备的本机包装中。没有任何服务器端(根本没有URL),所有请求都使用移动设备作为代理发送。在Javascript中使用OAuthSimple获取LinkedIn访问令牌

到目前为止,我设法:
- 获取请求令牌
- 将用户重定向到授权页面
- 得到授权PIN

我需要的示例代码,显示如何获得访问令牌使用OAuthSimple Javascript库
任何帮助将不胜感激。谢谢!

回答

6

不知道你是否是最近在我们的论坛上发布的同一个人(请参阅文章https://developer.linkedin.com/forum/oauthsimple-request-access-token),但我回复了演示代码,介绍了如何使用OAuthSimple完成此操作。

实际的示例代码可以在这里找到:https://gist.github.com/efc88a38da25ff4e9283

如果你使用它需要任何帮助,请不要犹豫,伸手!

-Jeremy

+0

是啊,这就是我! :D 我正要在LinkedIn的论坛上添加对该主题的引用! 再次感谢Jeremy! :) – Bert 2012-01-05 16:27:23

+0

不错的工作..我试了最后一个星期..现在它的工作 – 2014-04-10 09:29:13

+0

你可以在这里使用autentication + one call调用正确的完整javascript代码吗? – Shyghar 2014-11-21 16:13:15

0

这将创建一个PhoneGap的LinkedIn登录,它只是一个围绕代码工作,虽然,但这个工程ATLEAST我

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); 
相关问题