【问题标题】:Linkedin API: Exchange JSAPI Token for REST API OAuth TokenLinkedin API:将 JSAPI 令牌换成 REST API OAuth 令牌
【发布时间】:2016-01-19 08:27:39
【问题描述】:

我在将 JSAPI 令牌交换为 REST API 令牌时遇到了一些困难。我用这个作为参考:

https://developer-programs.linkedin.com/documents/exchange-jsapi-tokens-rest-api-oauth-tokens

我已经:在本地设置了一个自签名 SSL 证书,所以 Linkedin 的安全 cookie 可以正常工作;给我的应用 r_basicprofile 和 r_emailaddress 权限。

这是我的前端代码:

<script type="text/javascript" src="//platform.linkedin.com/in.js">
    api_key: **MY_CLIENT_ID**
    authorize: true
    credentials_cookie: true
</script>

...

$('.linkedin-signin').click(function(e) {       
    IN.User.authorize( function () {
        IN.API.Raw("/people/~").result(function(data) {
            $.post(location.origin+'/api/account/create/linkedin', { 'lId': data.id } ).done(function(result) {                 
                console.log(result);    
            });
        });
    });
    return false;
});

这是我的 PHP 代码,几乎与他们的文档中的完全一样:

$consumer_key = '**MY_CLIENT_ID**';
$consumer_secret = '**MY_CLIENT_SECRET**';
$cookie_name = "linkedin_oauth_${consumer_key}";
$credentials_json = $_COOKIE[$cookie_name]; 
$credentials = json_decode($credentials_json);

$access_token_url = 'https://api.linkedin.com/uas/oauth/accessToken';               

$oauth = new OAuth($consumer_key, $consumer_secret);
$access_token = $credentials->access_token;

// swap 2.0 token for 1.0a token and secret
$oauth->fetch($access_token_url, array('xoauth_oauth2_access_token' => $access_token), OAUTH_HTTP_METHOD_POST);

一切看起来都不错,但在 $oauth-&gt;fetch 上,我收到了错误:

OAuthException(code: 401): Invalid auth/bad request (got a 401, expected HTTP/1.1 20X or a redirect)

这让我相信令牌是无效的......但它是直接从 cookie 中获取的,那么它怎么可能是无效的呢?有什么想法吗?

【问题讨论】:

  • 您有幸找到解决该问题的方法吗?
  • 很遗憾没有。我有点恼火的是,Linkedin 在这里指导人们寻求技术支持,但没有回答大部分问题。我放弃了,将 Linkedin 排除在我网站的社交登录选项之外。
  • 我遇到了同样的问题,这个developer-programs.linkedin.com/documents/…坏了,我想也许LinkedIn刚刚删除了这个API
  • 哈,看那个。有趣的是,他们将人们引导至 StackOverflow 寻求支持,但不在这里回复问题。
  • 有没有人得到这个?这很令人沮丧

标签: api oauth linkedin


【解决方案1】:

今天我们也遇到了奇怪的 401 错误,显然是linkedin 坏了,因为一个小时后它又工作了,我们这边没有任何变化。

虽然我找到了这个网站,尽管它是一个非常旧的帖子,但我还是愿意分享我们如何修复它,它是有效的。

JS 前端

var AppConfig = {
    linkedin : {
        onLoad : "linkedinLibInit",
        api_key : 'YOUR_API_KEY',
        authorize : false,
        credentials_cookie: true
    }
};

window.linkedinLibInit = function ( response ) {
    // post init magic

    // cleanup window callback function
    delete window.linkedinLibInit;
}

$.getScript( "//platform.linkedin.com/in.js?async=true", function success() {
    IN.init( AppConfig.linkedin );
} );


function connectToLinkedIn() {
    if ( IN.User.isAuthorized() ) {
        _linkedinAuthorized();
    }
    else {
        IN.User.authorize( _linkedinAuthorized );
    }
}

function _linkedinAuthorized() {
    IN.API.Profile( "me" )
        .fields( 'id', 'first-name', 'last-name', 'location', 'industry', 'headline', 'picture-urls::(original)', 'email-address' )
        .result( function ( response ) {
            var accessToken = JSON.parse( $.cookie( 'linkedin_oauth_' + AppConfig.linkedin.api_key ) );
            // performApi Call to backend
        } )
        .error( function ( err ) {
            // render error
        } );
}

使用 PECL oAuth 的 PHP 后端

function offlineAuthLinkedIn($accessToken, $linkedinConfig) {
    $oAuth = new \OAuth( $linkedinConfig['app_id'], $linkedinConfig['app_secret'] );
    $oAuth->fetch(
        'https://api.linkedin.com/uas/oauth/accessToken',
        array('xoauth_oauth2_access_token' => $accessToken),
        OAUTH_HTTP_METHOD_POST
    );
    $response = null;
    parse_str($oAuth->getLastResponse(), $response);

    $oAuth->setToken($response['oauth_token'], $response['oauth_token_secret']);
    $oAuth->fetch(
        'http://api.linkedin.com/v1/people/~:(id,first-name,last-name,formatted-name,headline,location,picture-url,picture-urls::(original),public-profile-url)',
        array(),
        OAUTH_HTTP_METHOD_GET,
        array('x-li-format' => 'json')
    );
    $profile = json_decode($oAuth->getLastResponse(), true);
    $profile['user_id'] = $profile['id'];
    if (true == isset($profile['pictureUrl']))
    {
        $profile['profile_image'] = $profile['pictureUrl'];
        unset($profile['pictureUrl']);
    }
    return $profile;
}

【讨论】:

    猜你喜欢
    • 2015-04-25
    • 2014-05-10
    • 2017-12-20
    • 1970-01-01
    • 2015-07-24
    • 2020-02-29
    • 2011-02-18
    • 1970-01-01
    • 2020-05-06
    相关资源
    最近更新 更多