2
votes

I am working with linkedin API. I have followed the instructions given at https://developer.linkedin.com/docs/oauth2 - so I have got the code, exchanged that for an access token. All good.

Now when I try to hit the API, for example to post an update, "Step 4 — Make authenticated requests" I always get an error returned from the API saying my access token is invalid.

My Header Params sent to the API are this:

array(5) {
  [0]=>
  string(22) "Connection: Keep-Alive"
  [1]=>
  string(137) "Authorization: Bearer AQS888888888888888888888888888888-Ab2HY"
  [2]=>
  string(30) "Content-Type: application/json"
  [3]=>
  string(17) "x-li-format: json"
  [4]=>
  string(19) "Content-Length: 171"
}

The API response always gives me this:

string(134) "{
  "errorCode": 0,
  "message": "Invalid access token.",
  "requestId": "IRBXBZ1X5V",
  "status": 401,
  "timestamp": 1468498680913
}"

My debug from curl_getinfo() returns this data:

array(22) {
  ["url"]=>
  string(29) "https://api.linkedin.com/v1/?"
  ["content_type"]=>
  string(30) "application/json;charset=UTF-8"
  ["http_code"]=>
  int(401)
  ["header_size"]=>
  int(495)
  ["request_size"]=>
  int(466)
  ["filetime"]=>
  int(-1)
  ["ssl_verify_result"]=>
  int(0)
  ["redirect_count"]=>
  int(0)
  ["total_time"]=>
  float(0.275796)
  ["namelookup_time"]=>
  float(0.003589)
  ["connect_time"]=>
  float(0.00685)
  ["pretransfer_time"]=>
  float(0.055621)
  ["size_upload"]=>
  float(171)
  ["size_download"]=>
  float(134)
  ["speed_download"]=>
  float(485)
  ["speed_upload"]=>
  float(620)
  ["download_content_length"]=>
  float(134)
  ["upload_content_length"]=>
  float(171)
  ["starttransfer_time"]=>
  float(0.275764)
  ["redirect_time"]=>
  float(0)
  ["certinfo"]=>
  array(0) {
  }
  ["redirect_url"]=>
  string(0) ""
}

I am posting and expecting a response in JSON rather than XML.

See https://developer.linkedin.com/docs/share-on-linkedin for the docs I am following

I am confident that the access token is correct, I suspect I am not passing it correctly in the header "Authorization: Bearer", or my endpoint URL is not quite correct, I have tried this too:

https://api.linkedin.com/v1/people/~/shares?format=json

and

https://api.linkedin.com/v2/people/~/shares?format=json

Any ideas? NOTE I have removed most of the access token here, don't ask me to publish it!

2
I had the same problem an noticed that the access_token changes and that i had to refresh it. - D.L

2 Answers

3
votes

OK, so this is odd. My access tokens were being returned (what i assumed was correct). After allot of head scratching I decided to double check everything using the API console at apigee.com. I checked the access token and guess what, it was twice the length of all the tokens I had been receiving from Linkedin Oauth2.. Strange.

So I hard code this new access token into my system - boom! Everything works (Why do I ever doubt my code!) So I check everything, why is my access code being truncated?? DB - Good. Code - good. Very strange. I decide to re-hit the auth api to get a new access token (somthing I must have already done about 100 times!) Hey Presto! I now have a long access code.

I have no idea what the problem was, but my code is now working.

Sorry to say this to you linkedin, but your documentation leaves allot to be desired, when you change your api spec, you need to update your docs too. I have had allot of problems dealing with the API. Also if you are going to move your support department to Stackoverflow, good practices dictate that you monitor the requests. For anyone interested, the library's listed by Linkedin do not work after the changes made spring 2016. Main problem is that all the scopes have been limited.

1
votes

I had the same problem and the answer to it was in this: The length of Access Tokens is ~500 characters. We recommend that you plan for your application stack to handle tokens with length of at least 1000 characters in order to accommodate current and any future expansion plans. This applies to both Access Tokens as well as Refresh Tokens.

In my case I'm using Symfony 3.4 and Doctrine 2 as ORM, I had this snipet of code in the annotations:

/**
 * @var string
 *
 * @ORM\Column(name="access_token", type="string", length=255)
 */
private $accessToken;

As you can see I wasn't storing the full access token in Database for making authenticated requests, so changing the code above by:

  /**
 * @var text
 *
 * @ORM\Column(name="access_token", type="text")
 */
private $accessToken;

And everything started working like a charm..... I hope this Helps!!!!!