1
votes

I am using the article located at https://developer-programs.linkedin.com/documents/exchange-jsapi-tokens-rest-api-oauth-tokens to exchange my Javascript access token to a REST OAuth token.

After following the directions here, no matter what I seem to do, I only get a 400 Bad Request response back.

The flow I use for Facebook and want to recreate with LinkedIn is; front end authenticates to LinkedIn and passes an access token to my API, the API then gets all necessary user information and passes my own bearer token back to the client, et voila.

Unfortunately LinkedIn doesn't play so nicely with this, and I need to convert my token to an OAuth token from its Javascript token.

I pass the cookie LinkedIn gives me to my API, it looks something like the below (where OAuthBase is http://oauth.googlecode.com/svn/code/csharp/OAuthBase.cs)

access_token: "oxmKI9aU4RCfksdegZ3obZGHK-vo6Q4-4FSQk"
member_id: "AmjWCF7ExN"
signature: "t8KEbLjJ+r6uM42tUwfJm5yWp70="
signature_method: "HMAC-SHA1"
signature_order: ["access_token","member_id"]
signature_version: "1"

I then am attempting to make a call to https://api.linkedin.com/uas/oauth/accessToken to do the actual exchange. My code for this is:

public async Task<IHttpActionResult> ConvertLinkedInToken(LinkedInCovertTokenObject val)
    {
        string normalizeduri;
        string normalizedparams;            

        OAuthBase o = new OAuthBase();
        string signature = o.GenerateSignature(new Uri("https://api.linkedin.com/uas/oauth/accessToken"), Startup.linkedInAuthOptions.ClientId, Startup.linkedInAuthOptions.ClientSecret, val.access_token, null, "POST", o.GenerateTimeStamp(), o.GenerateNonce(), out normalizeduri, out normalizedparams);

        var client = new HttpClient();
        var uri = new Uri("https://api.linkedin.com/uas/oauth/accessToken?" +
            "oauth_consumer_key=" + Startup.linkedInAuthOptions.ClientId +
            "&xoauth_oauth2_access_token=" + val.access_token +
            "&signature_method=HMAC-SHA1" +
            "&signature=" + signature
        );

        var response = await client.GetAsync(uri);

        return Ok();
    }

No matter how I play around all I get back from LinkedIn is a 400 Bad Request without any other useful information.

1) How can I convert LinkedIn JS token to Rest OAuth token in my c# api

2

2 Answers

3
votes

This is how I achieved that:

On the frontend:

IN.User.authorize(function(){
  // here you can find oauth token
  var oauth_token = IN.ENV.auth.oauth_token;
  // send this token to your API endpoint
});

On your API (curl example), of course replace OAUTH_TOKEN with token received on the frontend.

curl -X GET \
  'https://api.linkedin.com/v1/people/~:
(id,firstName,lastName,siteStandardProfileRequest,picture-url,email-
address)?format=json' \
  -H 'oauth_token: OAUTH_TOKEN'
1
votes

You are looking at old documentation from LinkedIn. Starting from 12th May, LinkedIn has started rolling out new changes in their API which includes authentication. In my knowledge, LinkedIn is not using OAuth anymore, and you need OAuth2.0 henceforth for authentication. You should check this link for more information: https://developer.linkedin.com/docs/signin-with-linkedin