Linkedin Oauth Issue - oauth_verifier

天大地大妈咪最大 提交于 2019-12-11 09:24:15

问题


Having some problems trying to exchange my JSAPI tokens for REST Oauth Tokens (https://developer.linkedin.com/documents/exchange-jsapi-tokens-rest-api-oauth-tokens)

I'm using this library - http://code.google.com/p/oauth-php/ - As opposed to the PECL extension as I'm unable to install extensions onto the server.

There seem to be plenty of similar questions, but none which actually answer the question - How to use the above library to authenticate with Linkedin.

My code is as follows:

    $cookie_name        = "linkedin_oauth_" . $this->_c->linkedin_api_key;
    $credentials_json   = stripslashes($_COOKIE[$cookie_name]);
    $credentials        = json_decode($credentials_json);

    // Get the Access Token + Secret
    $access_token_url = 'https://api.linkedin.com/uas/oauth/accessToken';

    OAuthStore::instance("2Leg", array(
        'consumer_key'      => $this->_c->linkedin_api_key,
        'consumer_secret'   => $this->_c->linkedin_api_secret
    ));

    try {

        $request = new OAuthRequester($access_token_url, 'POST', array(
            'xoauth_oauth2_access_token' => $credentials->access_token
        ));

        $result = $request->doRequest();

    } catch(OAuthException2 $e) {

        print_r($e->getMessage());

    }

The catch statement outputs:

Request failed with code 400: oauth_problem=parameter_absent&oauth_parameters_absent=oauth_verifier

How do I get this oauth_verifier? It was my understanding that I shouldn't need it if I was passing the xoauth_oauth2_access_token already?

I've checked all of the variables I.E. $credentials and $this->_c and all of the variables are passing through correctly.


回答1:


This is actually a bug in the oauth-php library. The library is incorrectly handling parameters which are prefixed with xoauth_* and treating them the same way it handles oauth_* parameters. This is in violation of the OAuth spec and most (all?) other OAuth libraries don't have this issue. The fix is to do the following:

Inside the file OAuthRequestSigner.php, find the following:

1) inside the getAuthorizationHeader function, find the line which reads:

if (strncmp($name, 'oauth_', 6) == 0 || strncmp($name, 'xoauth_', 7) == 0)

and change it to be:

if (strncmp($name, 'oauth_', 6) == 0)

2) Inside the function getQueryString, find the line which reads:

||  (strncmp($name, 'oauth_', 6) != 0 && strncmp($name, 'xoauth_', 7) != 0))

and change it to be:

||  (strncmp($name, 'oauth_', 6) != 0)

After that, all you need to do is essentially the same as you were already doing, which is the following:

try {

    $request = new OAuthRequester($access_token_url, "POST", array('xoauth_oauth2_access_token' => $access_token));

    $result = $request->doRequest();

    var_dump($result);

} catch(OAuthException2 $e) {

    print_r($e->getMessage());

}

And you should be all set. If you have any further issues, please do not hesitate to reach out on our developer forums and either myself or someone else on the team would be happy to help.

Enjoy!

-Jeremy



来源:https://stackoverflow.com/questions/10123254/linkedin-oauth-issue-oauth-verifier

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!