Bod has asked for the wisdom of the Perl Monks concerning the following question:

I am trying to connect to LinkedIn using LWP::Authen::OAuth2. Authorisation goes fine but when I come to exchange the authorisation token for an access token, I get this error:

Endpoint: https://api.linkedin.com/v2/accessToken JSON: { "serviceErrorCode":65604, "message":"Empty oauth2 access token", "status":401 }
That doesn't seem to make alot of sense to me as I would expect the OAuth2 Access Token to be empty in a request to get it!

This is the bare bones of what I am doing...

my $linkedin = LWP::Authen::OAuth2->new( client_id => 'xxxxxxx', client_secret => 'xxxxxxx', authorization_endpoint => 'https://api.linkedin.com/uas/oauth2/a +uthorization', token_endpoint => 'https://api.linkedin.com/v2/accessTok +en', redirect_uri => "https://$ENV{'HTTP_HOST'}/cgi-bin/pos +tdog.pl?command=authorize_linkedin", scope => 'w_member_social', save_tokens => \&save_linkedin_token, ); ######################### # LinkedIn button clicked sub linkedin { my $auth_url = $linkedin->authorization_url; print "Location: $auth_url\n\n"; exit 0; }
The code above behaves as expected by going off to LinkedIn, authorising the app and calling the callback URL.
The callback URL does this:
sub authorize_linkedin { my $token = $linkedin->request_tokens( code => $data{'code'}, ); print "Content-type: text-plain\n\n"; print "ERROR: $data{'error'}\n\nMessage: $data{'error_description' +}\n\n"; print "TOKEN: $token\n"; print $data{'code'}; exit 0; }
The error (above) is generated at the request_tokens call. $data{'code'} contains the code passed as a query parameter to the callback URL.

I feel I must be missing something obvious here...

Replies are listed 'Best First'.
Re: Obtaining OAuth2 Access Token
by Bod (Parson) on Apr 17, 2021 at 11:42 UTC

    Here is a bit more meat on the question with a working Twitter example:

    I have a simple working system - written a long time ago so go easy on my code! - that allows any Twitter user to see who they follow and who follows them back and follow/unfollow them from that list. You can see this on my website and it demonstrates the workflow for OAuth2 albeit using Net::Twitter to do the connection work.

    Here is the code that does the work with Twitter taken from the live site:

    That shows an OAuth2 workflow that functions as it should.

    For LinkedIn the general arrangement should work the same. Except that LinkedIn only passes back a code parameter - it doesn't pass back oauth_token or oauth_verifier. Instead the error is Token endpoint missing expected token_type in successful response. with a payload of:

    { "serviceErrorCode": 65604, "message": "Empty oauth2 access token", "status": 401 }
    The LinkedIn API documentation doesn't seem very helpful to me.

    Here is what I am trying...you can see this working on my test website. This runs exactly this code but with real keys. The callback URL is authorised to be used within LinkedIn.

    The require just populates %data with the key/value pairs from the query string.

Re: Obtaining OAuth2 Access Token
by Bod (Parson) on Apr 18, 2021 at 20:53 UTC