api - 领英 API : Exchange JSAPI Token for REST API OAuth Token

标签 api oauth linkedin

我在将 JSAPI token 交换为 REST API token 时遇到了一些困难。我正在使用它作为引用:

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->fetch ,我收到错误:
OAuthException(code: 401): Invalid auth/bad request (got a 401, expected HTTP/1.1 20X or a redirect)

这让我相信 token 是无效的……但它是直接从 cookie 中获取的,那么它怎么会是无效的呢?有任何想法吗?

最佳答案

今天我们也遇到了奇怪的 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;
}

关于api - 领英 API : Exchange JSAPI Token for REST API OAuth Token,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33240716/

相关文章:

php - 如何处理 CURL 中服务器重置的连接

node.js - 使用 react js 和 express API 服务器使用 fetch 发布一个对象

ruby - 公司目录的 LinkedIn API

api - 我在哪里可以下载所有必要的类来编写 Hadoop MapReduce 作业?

php - 导入时,二进制 IP 地址上的 API 调用显示奇怪的格式

java - 使用 Java OAuth 进行购物的内容 API

javascript - 谷歌 OAuth 2.0 同源错误

r - 在R的httr包中使用oauth2.0 token

javascript - LinkedIn API - 如何在公司A中找到用户1和用户2之间的相互连接

android - LinkedIn Android SDK 设置