Dormouse
Dormouse

Reputation: 5198

LinkedIn API OAuth Problem

I'm using the PEAR OAuth Class to access the LinkedIn developer API and I've come across a bit of a problem. I can authorize my application but when it comes to getting an accessToken I'm receiving this error:

Edit:

Code after Adam's suggestions

public function oauth_access()
{
    session_start();

    $token = $_GET['oauth_token'];
    $verifier = $_GET['oauth_verifier'];
    $secret = $_SESSION['trequest_token_secret'];

    $key = "****";
    $secret = "****";

    $oauthc = new OAuth($key, $secret, OAUTH_SIG_METHOD_HMACSHA1, OAUTH_AUTH_TYPE_AUTHORIZATION);

    $oauthc->setToken($token, $secret);
    $oauthc->setNonce(rand());


    try
    {
        $access_token_info = $oauthc->getAccessToken("https://api.linkedin.com/uas/oauth/accessToken");
        $_SESSION['laccess_oauth_token']= $access_token_info['oauth_token'];
        $_SESSION['laccess_oauth_token_secret']= $access_token_info['oauth_token_secret'];
        $_SESSION['loauth_verifier'] = $verifier;

    }
    catch (OAuthException $e)
    {
       echo $e->getMessage();
    }
}

But I'm now getting a different error:

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

Upvotes: 1

Views: 2571

Answers (1)

Adam Trachtenberg
Adam Trachtenberg

Reputation: 2241

You don't need to manually compute the signature, as pecl/oauth will do that for you.

Also, you're telling the library to pass the data in the Authorization HTTP header. That is a good place to have it. Then you are passing it via a query parameter. That is permitted, but less optimal. (You may actually be passing it in two places.) Also, pecl/oauth will automatically generate the proper timestamp.

When I first started, I found this blog post to be a good first start.

Or you can use the LinkedIn PHP library listed by Paul. It's also a good place to begin, if you don't want to reuse pecl/oauth because you're using that someplace else.

Upvotes: 2

Related Questions