在 Javascript 中使用 OAuthSimple 获取 LinkedIn 访问令牌

发布于 2024-12-23 12:11:22 字数 344 浏览 1 评论 0原文

我在 Javascript 中使用 OAuthSimple 以及基于 PIN 的身份验证(OOB 流程)。 我们正在使用 PhoneGap 开发一个 HTML5 应用程序,该应用程序位于移动设备的本机包装器内。没有任何服务器端(根本没有 URL),所有请求都是使用移动设备作为代理发送的。

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

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

I'm using OAuthSimple in Javascript with PIN based authentication (OOB flow).
We are developing an HTML5 app which lives inside a mobile device's native wrapper using PhoneGap. There's not ANY server side (no URL at all), all requests are sent using the mobile device as a proxy.

So far I managed to:
- Get Request Token
- Redirect user to authorization page
- Got authorization PIN

I need sample code that shows how to get an Access Token using OAuthSimple Javascript library.
Any help will be appreciated. Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

沧笙踏歌 2024-12-30 12:11:22

不确定您是否是最近在我们的论坛上发帖的同一个人(请参阅此处的帖子 https ://developer.linkedin.com/forum/oauthsimple-request-access-token),但我回复了演示代码,了解如何使用 OAuthSimple 做到这一点。

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

如果您需要如果需要使用它的任何帮助,请随时与我们联系!

-杰里米

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.

The actual sample code can be found here: https://gist.github.com/efc88a38da25ff4e9283

If you need any help using it, don't hesitate to reach out!

-Jeremy

你是暖光i 2024-12-30 12:11:22

这将创建一个phonegap linkedIn登录,虽然它只是一个解决代码,但这至少对我有用

    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);

This will create a phonegap linkedIn login, its just a worked around code though, but this works atleast for me

    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);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文