54

Is there an easy way to grab a users LinkedIn profile photo?

Ideally similar to how you would with Facebook - http://graph.facebook.com/userid/picture

General Grievance
  • 4,555
  • 31
  • 31
  • 45
George Wiscombe
  • 1,422
  • 1
  • 13
  • 13
  • 3
    Make note of [ccoloma's answer](https://stackoverflow.com/a/53886138/1747491) below, as this has changed **a lot** with `v2` of the API. The accepted and top-voted answers are for `v1`, which will be deprecated soon. – theblang Feb 06 '19 at 23:48

11 Answers11

52

You can retrieve the original photo size with this call:

http://api.linkedin.com/v1/people/~/picture-urls::(original)

Note that this could be any size, so you'll need to do scaling on your side, but the image is the original one uploaded by the user.

stwienert
  • 3,402
  • 24
  • 28
Rahim Basheer
  • 521
  • 1
  • 4
  • 2
35

Not as easy... You need to go through OAuth, then on behalf of the member, you ask for:

http://api.linkedin.com/v1/people/{user-id}/picture-url

abraham
  • 46,583
  • 10
  • 100
  • 152
Adam Trachtenberg
  • 2,231
  • 1
  • 14
  • 18
  • 13
    That's a shame, had hoped there was an easier mechanism. – George Wiscombe Aug 07 '11 at 10:04
  • 1
    so we do need to do two calls, the first one to get the picture url, and the second one to get the actual picture? – saintmac Dec 31 '11 at 15:37
  • 2
    saintmac. No if you only want hte picture just call this url. the {user-id} is a variable and "picture-url" is actual text you put in the url to get the picture. However in my testing this returns the smaller picture not the original. I recommend the answer below using picture-urls::(original) – danielson317 Oct 09 '13 at 18:30
  • 2
    it is not displaying the image.i might b doing something wrong.any help? – Sougata Bose Oct 30 '13 at 10:23
  • Is there way with having to auth? – Lounge9 Jul 11 '14 at 05:07
  • 2
    works for me in 2016, you probably need https instead of http and an Authorization header, using a Bearer access_token, so in your http headers put "Authorization" : "Bearer " – Alexander Mills Feb 20 '16 at 22:53
  • In my case, I needed to enable HTTPS. I was getting the "need SSL" error. – patrickGranite Feb 07 '18 at 17:15
20

If you use the 2.0 version of the API (all developers need to migrate by March 1, 2019), you should use projections to expand the profilePicture.displayImage. If you do this, you will have a full JSON element displayImage~ (the '~' is not a typo) inside profilePicture with all the info you may need.

https://api.linkedin.com/v2/me?projection=(id,profilePicture(displayImage~:playableStreams))

You can see more at the Profile Picture API doc to look at the JSON response or the Profile API doc.

ccoloma
  • 301
  • 2
  • 5
12

Once the Linkedin user authentication using OAuth 2.x is done, make a request to the people URL.

https://api.linkedin.com/v1/people/~:(id,email-address,first-name,last-name,formatted-name,picture-url)?format=json

Where ~ stands for current authenticated user. The response will be something like this ...

{
  "id": "KPxRFxLxuX",
  "emailAddress": "johndoe@example.com",
  "firstName": "John",
  "lastName": "Doe",
  "formattedName": "John Doe",
  "pictureUrl": "https://media.licdn.com/mpr/mprx/0_0QblxThAqcTCt8rrncxxO5JAr...cjSsn6gRQ2b"
}

Hope this helps!

Madan Sapkota
  • 25,047
  • 11
  • 113
  • 117
6

I'm using OWIN in my solution so after the user allows your application to use LinkedIn credentials a simple and plain GET request to URL https://api.linkedin.com/v1/people/~:(picture-URL)?format=json, as explained before with a Bearer authorization in request headers, solved my problems.

My Startup.Auth.cs file

var linkedInOptions = new LinkedInAuthenticationOptions()
{
   ClientId = [ClientID],
   ClientSecret = [ClientSecret],
   Provider = new LinkedInAuthenticationProvider()
   {
      OnAuthenticated = (context) =>
      {
          // This is the access token received by your application after user allows use LinkedIn credentials
          context.Identity.AddClaim(new Claim(
              "urn:linkedin:accesstoken", context.AccessToken));
          context.Identity.AddClaim(new Claim(
              "urn:linkedin:name", context.Name));
          context.Identity.AddClaim(new Claim(
              "urn:linkedin:username", context.UserName));
          context.Identity.AddClaim(new Claim(
              "urn:linkedin:email", context.Email));
          context.Identity.AddClaim(new Claim(
              "urn:linkedin:id", context.Id));

          return Task.FromResult(0);
      }
   }
};

app.UseLinkedInAuthentication(linkedInOptions);

My method to get the user's profile picture in LinkedIn:

public string GetUserPhotoUrl(string accessToken)
{
   string result = string.Empty;
   var apiRequestUri = new Uri("https://api.linkedin.com/v1/people/~:(picture-url)?format=json");
   using (var webClient = new WebClient())
   {
      webClient.Headers.Add(HttpRequestHeader.Authorization, "Bearer " + accessToken);
      var json = webClient.DownloadString(apiRequestUri);
      dynamic x = JsonConvert.DeserializeObject(json);
      string userPicture = x.pictureUrl;
      result = userPicture;
   }
   return result;
}

And finally a snippet of my action that consumes the method above:

public async Task<ActionResult> ExternalLoginCallback(string returnUrl)
{
   ...
   var externalIdentity = HttpContext.GetOwinContext().Authentication.GetExternalIdentityAsync(DefaultAuthenticationTypes.ExternalCookie);
   string accessToken =
               externalIdentity.Result.Claims.FirstOrDefault(c => c.Type == "urn:linkedin:accesstoken").Value;
   model.PhotoUrl = GetUserPhotoUrl(accessToken);
   ...
}

I hope it could help. Best regards

diegosousa88
  • 91
  • 1
  • 6
4

This works well for me!

Explained -

This is for a thumbnail with all other data-

https://api.linkedin.com/v1/people/~:(id,location,picture-urls::(original),specialties,public-profile-url,email-address,formatted-name)?format=json

This is for original image with all other data -

https://api.linkedin.com/v1/people/~:(id,location,picture-url,specialties,public-profile-url,email-address,formatted-name)?format=json

Just use picture-urls::(original)instead of picture-url !

This is currently being used in Gradbee

arora
  • 869
  • 7
  • 12
  • getting formatted-name (null) (null) with url for original image with all other data - any idea? – Krutika Sonawala Sep 22 '17 at 10:13
  • @KrutikaSonawala there might be some problem with the linkedin account that you are authorizing. check this link with your own linkedin profile `https://apigee.com/console/linkedin` – arora Nov 07 '17 at 11:29
2

When you login to linkedin, you will get accesstoken. Use that access token and you can retrieve users data

 LinkedInApiClient client = factory.createLinkedInApiClient(accessToken);
                        com.google.code.linkedinapi.schema.Person person = client.getProfileForCurrentUser(EnumSet.of(
                                ProfileField.ID, ProfileField.FIRST_NAME, ProfileField.LAST_NAME, ProfileField.HEADLINE,
                                ProfileField.INDUSTRY, ProfileField.PICTURE_URL, ProfileField.DATE_OF_BIRTH,
                                ProfileField.LOCATION_NAME, ProfileField.MAIN_ADDRESS, ProfileField.LOCATION_COUNTRY));
    String imgageUrl=person.getPictureUrl();
Kimmi Dhingra
  • 2,249
  • 22
  • 21
1

If your goal is simply to display the photo on your site then the LinkedIn Member Profile Plugin may work out for you. It will display the photo, some additional info, along with LinkedIn branding.

Since the LinkedIn API is designed to be used only on behalf of the current logged in user it does not offer similar functionality as the facebook graph api.

Hoodah
  • 31
  • 3
1

This is my solution and it works very very well:

def callback(self):
    self.validate_oauth2callback()
    oauth_session = self.service.get_auth_session(
        data={'code': request.args['code'],
              'grant_type': 'authorization_code',
              'redirect_uri': self.get_callback_url()},
        decoder=jsondecoder
    )
    me = oauth_session.get('people/~:(id,first-name,last-name,public-profile-url,email-address,picture-url,picture-urls::(original))?format=json&oauth2_access_token='+str(oauth_session.access_token), data={'x-li-format': 'json'}, bearer_auth=False).json()
    social_id = me['id']
    name = me['firstName']
    surname = me['lastName']
    email = me['emailAddress']
    url = me['publicProfileUrl']
    image_small = me.get('pictureUrl', None)
    image_large = me.get('pictureUrls', {}).get('values', [])[0]
    return social_id, name, surname, email, url, image_small, image_large, me
piezzoritro
  • 141
  • 2
  • 14
0

For me this works

image= auth.extra.raw_info.pictureUrls.values.last.first

with omniauth-linkedin gem

TylerH
  • 20,799
  • 66
  • 75
  • 101
sparkle
  • 7,530
  • 22
  • 69
  • 131
0

This may not be quite what you're asking for, but it's useful for individual investigations.

Call up the page in Firefox, left-click the menu over the background image. Select Inspect Element(Q).

search for -target-image" That will be the end of of id attribute in an img element. The src attribute of that img element, will be the URL of the background image.