4

I'm actually trying to do a simple login with Linkedin and the new API V2 with Xamarin.Auth extension. I do get the token like this

var auth = new OAuth2Authenticator(
    clientId: *****,
    clientSecret: *****,
    scope: "r_liteprofile",
    authorizeUrl: new Uri("https://www.linkedin.com/oauth/v2/authorization"),
    redirectUrl: new Uri(*****),
    accessTokenUrl: new Uri("https://www.linkedin.com/oauth/v2/accessToken")
    );

but when I try to make a request it fails with an {"serviceErrorCode":100,"message":"Unpermitted fields present in PARAMETER: Data Processing Exception while processing fields [/access_token, /format]","status":403}. Code for these error:

var request = new OAuth2Request(
    "GET",
    new System.Uri("https://api.linkedin.com/v2/me?"
    + "format=json"
    + "&oauth2_access_token="
    + e.Account.Properties["access_token"]),
    null,
     e.Account
    );

var linkedinResponse = await request.GetResponseAsync();
var json = linkedinResponse.GetResponseText();
Console.WriteLine(json);

If I take out the fields, it fails because of empty access token: {"serviceErrorCode":65604,"message":"Empty oauth2 access token","status":401} Code for these error:

var request = new OAuth2Request(
                    "GET",
                    new System.Uri("https://api.linkedin.com/v2/me"),
                    null,
                    e.Account
                    );

var linkedinResponse = await request.GetResponseAsync();
var json = linkedinResponse.GetResponseText();

Console.WriteLine(json);

I've confirmed that I've recieved the token. Looking for a solution I found that the token must be in a header but I can't change or add any header. Can anyone help me please?

Thank you very much.

PD: Forgot to say this code is for Android but I needed to do it for iOS aswell.

Valli
  • 81
  • 5
  • 1
    The format and oauth2_access_token are unnecessary query parameters. The access token must be passed within the Authorization header in your request. – Christopher Ou Feb 21 '19 at 00:23
  • Based on your `status:401 Empty oauth2 access token`, The authentication header is missing or empty. Make sure the authentication header follows the format Authorization: Bearer (your access token).like this link.https://learn.microsoft.com/en-us/linkedin/shared/authentication/authorization-code-flow?context=linkedin/context#step-4-make-authenticated-requests – Leon Feb 21 '19 at 03:08
  • Yes, the problem is xamarin.auth extension doesn't let you change headers, or at least I haven't found how to do it. Is there any way to make it work without the extension in a xamarin app (android/iOS)? – Valli Feb 21 '19 at 08:17

1 Answers1

4

Hi finally I found the solution to this.

In Linkedin API V2 you have to change your AccessTokenparameterName.

yourOauth2Request.AccessTokenParameterName = "oauth2_access_token";

Hope I can help somebody to not get mad like me.

Valli
  • 81
  • 5