Recent Posts
- (09/10) Fixing Warning: the ECDSA host key for 'github.com' differs from the key for the IP addressTAGS:Web Server Admin
- (12/26) CakePHP 3 - Getting List of Column Definitions from a Table (like schema())TAGS:CakephpCake3
- (09/14) Change Order of Loaded Behaviors in CakePHP 3TAGS:Cake3CakephpWeb ProgrammingPhp
- (05/29) CakePHP 3 - Accept JSON Header Only Working When Debug FalseTAGS:Web ProgrammingCakephpCake3
- (05/23) Remove All Events from Google Calendar (Reset Calendar)TAGS:Web ProgrammingPhp
- (11/08) Google Tag Manager (GTM) Not Firing Default PageView EventTAGS:Web ProgrammingJavascriptGoogle Tag Manager
- (10/13) In PHP, how do you get __toString() Magic Method Result without calling echo?TAGS:CakephpCake3Cakephp 13PhpWeb Programming
- (11/14) Getting output from shell_exec() at all timesTAGS:Web ProgrammingWeb Server Admin
Subscribe to my feed
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 15,696 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:
- 'method' => $method,
- );
- $streamcontent['content'] = $body;
- $streamcontent['follow_location'] = 0; // HERE IT IS
- $streamcontent['max_redirects'] = 0; // FOR SAFE MEASURE (I WAS REALLY PISSED AND WANTED TO BE SURE)
- $streamcontent['header'] =
- 'Content-Type: application/json',
- 'x-li-format: json',
- );
- }
- $streamcontent,
- )
- );
- // Hocus Pocus
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!)
Tags for LinkedIn PHP Api - Fixing 401 Unknown Authentication Scheme Error
Errors | Php | Usage | Web Programming
Comments for this Posting
Sorry, comments are closed for this posting.
Please Email Kevin if you have any questions. Thanks!
Posted by Antony
on 31/7/14
Thank you soo much for this bit of code and well done for working it out!! Thanks.