1

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)

4
  • I'm just curious, but is there any reason for using the PEAR OAuth and not the OAuth library provided by LinkedIn? Commented Jun 2, 2011 at 11:02
  • I wasn't aware that there was an OAuth Library provided by LinkedIn? I've seen a lot of user built libraries but nothing official? Commented Jun 2, 2011 at 11:07
  • I've used the following library, attached to the forum post - developer.linkedin.com/thread/1439. It isn't without it's flaws but I can help you out further with that. Commented Jun 2, 2011 at 18:31
  • FYI, another PHP library: Simple-LinkedIn Commented Jun 4, 2011 at 18:13

1 Answer 1

2
+150

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.

Sign up to request clarification or add additional context in comments.

2 Comments

Hi adam thanks for answering. I've updated the code in the question to match what I have now but I'm still getting the same 401 error. I did use the same wordpress article as a starting point but even that doesn't seem to work for me.
As it turned out I was resetting a variable and so your answer was correct. Thanks very much as this had been ruining my life, have some bounty.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.