MainelyDesign.com Blog

LinkedIn PHP Api - Fixing 401 Unknown Authentication Scheme Error

Posted on 07/17/2014 at 09:55 am by Kevin Wentworth
Viewed 11,482 times | 1 comment

If you are a disciplined developer you probably take a lot of time to read the manual and probably also use the examples provided by the vendor, right?  This is what I was doing in my attempt to get the LinkedIn PHP API working.  LinkedIn recommends using file_get_contents() with a stream context.  Looks good to me.  After you get your GETs right and your posts POSTed everything was working just fine (love the new Oauth2 authentication scheme by the way!).  Yippee!  Hooray!  LinkedIn Auto-posting is here!

401 Unknown Authentication Scheme Error

Then, I tried to post a company share.  Simple enough, right?  Wrong.  This is where you start getting the wonderful 401 errors with an "Unknown Authentication Scheme" error.  The result from file_get_contents() is false and warnings are triggered.  So, you read the LinkedIn forum and try to add $streamcontext['ignore_errors'] = true.  Now you get a response but it's the 401 unauthorized error.  I was about to ignore the error and just look at the $http_response_header array for the status (which was a HTTP/1.0 201 Created) when I noticed something peculiar: another status at the end of $http_response_header with that 401 code and the message of Unknown Authentication Scheme!

LinkedIn Doesn't Return Any Content From Share POST

Why?  Don't ask me... I would like to see the ID of the article, but I guess that's too Old School.

LinkedIn Returns a Location Header from Share POST

What they do give you is the location to the as yet to be built "future" location of the post.  Quoting directly from the LinkedIn documentation: "Response: returns 201 Created on success. It will also provide a Location HTTP header with a URL for the created resource. However, at this time, you cannot retrieve the Share from that location. It's there for future compatibility."

Wait... Header You Say?

Yes, that's right, a Location HEADER!  Well, guess what file_get_contents will do by default?  Well, file_get_contents() FOLLOWS location headers by default!  (documentation here) That's the big issue with the provided API documentation: PHP (and file_get_contents()) will follow that Location: header and send you to some place that isn't really there.

Turn off file_get_contents() follow_location setting

The fix is easy: add follow_location = 0 to your stream context settings, like so:

  1. $streamcontent = array(
  2.                 'method' => $method,
  3.         );
  4.        
  5.         if(!empty($body)){
  6.             $streamcontent['content'] = $body;
  7.             $streamcontent['follow_location'] = 0; // HERE IT IS
  8.             $streamcontent['max_redirects'] = 0; // FOR SAFE MEASURE (I WAS REALLY PISSED AND WANTED TO BE SURE)
  9.             $streamcontent['header'] =
  10.                 array(
  11.                         'Content-Type: application/json',
  12.                         'x-li-format: json',
  13.                         'Content-Length: '.strlen($body),
  14.                 );
  15.         }
  16.  
  17.         $context = stream_context_create(
  18.                         array('http' =>
  19.                             $streamcontent,
  20.                         )
  21.                     );
  22.         // Hocus Pocus
  23.         $response = file_get_contents($url, false, $context);

Happy Coding!
-Kevin Wentworth

P.S. Sorry for all the exclamations!!! This has been driving me nuts! And, I figured it out all on my own (well, to be fair, I didn't find this solution in my travels on the Interwebs, but I'm sure some smart person has already figured this out!)

Bookmark and Share

Tags for LinkedIn PHP Api - Fixing 401 Unknown Authentication Scheme Error

Errors | Php | Usage | Web Programming

Comments for this Posting

Posted by Antony

on 31/7/14

Thank you soo much for this bit of code and well done for working it out!! Thanks.

Sorry, comments are closed for this posting.

Please Email Kevin if you have any questions. Thanks!

Meet Site Avenger - Hosted Content Management System

Powered By: Site Avenger | Site Production: Saco Design