Question

Users can log in to my Rails app using their LinkedIn account thanks to OAuth. However, I am having trouble displaying the user's profile image. The following URL does not load a picture:

<%= image_tag("http://api.linkedin.com/v1/people/{user-id}/picture-url") %>

How can I get the user's LinkedIn profile image to display in my Rails app?

Thanks!

Was it helpful?

Solution

Try to get the original picture with:

http://api.linkedin.com/v1/people/{user-id}/picture-urls::(original)

Update:

From current docs (recommend to read it):

Using current user (after user logged in):

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

Using member_id:

http://api.linkedin.com/v1/people/id=12345:(picture-url)

Public profile:

http://api.linkedin.com/v1/people/url=<public-profile-url>:(picture-url)

Those URLs return xml, so you could parse the xml response to get picture-url string and use it as a param for image_tag. Alternatively, you can retrieve info as a json passing an extra param like:

http://api.linkedin.com/v1/people/~:(picture-url)?format=json

In both cases (xml or json), you need to extract the picture-url from api response for passing it to image_tag.

This gem omniauth-linkedin-oauth2 could probably help you.

OTHER TIPS

This is my solution working perfectly:

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 = 'linkedin$' + 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
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top