Getting Google+ profile picture url with user_id
Asked Answered
H

12

98

I know that lots of social network APIs provide a way to construct a url to the profile picture of a user, using their user_id or username. For Facebook it looks like this:

http://graph.facebook.com/user_id/picture?type=square

Now is there something like this for Google Plus? Or any other way to get user's pic without an API call??

Haeres answered 3/2, 2012 at 12:29 Comment(3)
I opened a feature request which you can star to get updates if Google ever implements it. code.google.com/p/google-plus-platform/issues/detail?id=167Democrat
Here's an example for the other way around. Embedding for instance your StackOverflow flair in your Google+ Profile: goo.gl/8EE4LTPrecipitin
The answer to this question changed since 2012 too many times. I posted one answer that work in 2020 - don't know how much time :)Flare
D
103

Google had changed their policy so the old way for getting the Google profile image will not work now, which was

https://plus.google.com/s2/photos/profile/(user_id)?sz=150

New Way for doing this is

Request URL

https://www.googleapis.com/plus/v1/people/115950284...320?fields=image&key={YOUR_API_KEY}

That will give the Google profile image url in json format as given below

Response :

{
    "image": 
    {
         "url": "https://lh3.googleusercontent.com/-OkM...AANA/ltpH4BFZ2as/photo.jpg?sz=50"
    }
}

More parameters can be found to send with URL which you may need from here

For more detail you can also check the given question where I have answered for same type of problem How to get user image through user id in Google plus?

Dean answered 20/3, 2014 at 11:48 Comment(10)
It is still not a redirect, it cannot be used within a <img> tagBlossom
that's why its mentioned in JSON format don't you get that, since there are changes so you can't expect everything to work as per before @KirillKulakovWong
I'm not saying it's wrong, I'm just saying that is not as useful as the old method, and besides the OP asked for ''''way to get users pic without an API call''''Blossom
yup that's right this method will not return user's image, even I am searching for same answer. @KirillKulakovWong
@TheMohanAhuja, does the API req take a param to request for a particular image size? Looks like it defaults to 50px.Oceania
@Oceania you can't request in API call but you surely can add size with image URL, that you got as .../-OkM...AANA/ltpH...as/photo.jpg?sz=512Wong
Thanks works. But as I could do if I have only the email, not the id?Actinomorphic
i having a bad request error, what api should i enable on my developer consoleTaffeta
Will the generated image.url expired?Vestment
is there any way by which we can get the profile picture of the user without google plus api since it will be deprecated in futureSteiermark
P
94

UPDATE: The method below DOES NOT WORK since 2015

It is possible to get the profile picture, and you can even set the size of it:

https://plus.google.com/s2/photos/profile/<user_id>?sz=<your_desired_size>

Example: My profile picture, with size set to 100 pixels:

https://plus.google.com/s2/photos/profile/116018066779980863044?sz=100

Usage with an image tag:

<img src="https://plus.google.com/s2/photos/profile/116018066779980863044?sz=100" width="100" height="100">

Hope you get it working!

Pash answered 27/6, 2012 at 16:10 Comment(19)
Is this documented anywhere?Proscription
I found it in their source code, and since I figure lots of other services already use them I think it's unlikely that they will change.Norwich
Nice find.Saves me a lot of trouble.Grow
@EmilStenström using your examples works perfectly but the image is small and when you increase it, it becomes blurry.Is there a waya round this?Afternoon
@OjonugwaOchalifu: No. The blurriness is because the original user uploaded a small image, there is no way to get a bigger version of a small image. If you check my example above you see it doesn't get blurry even on 400.Norwich
As a matter of fact I used your Id and retrieved your profile picture.with the same result.Afternoon
@OjonugwaOchalifu: My comment still stands.Norwich
But just so we are on the same page, is this tutorial based on Android? Because am trying this in Android.Afternoon
Android is trying to reduce the image quality to reduce bandwidth. The screen is small, do not need a big picture.Trefor
This URL worked for a while, but now only seems to work for me when I am NOT signed in to google. When I am signed in, I get a 404 error. Anyone else have the same results?Lieu
Yes, not working right now, anybody knows if it's a temporal issue or permanent one?Papst
I found that this alternative still works: s2.googleusercontent.com/s2/photos/profile{id}?sz={size} (per answer below by Will Norris)Papst
These URLs are not working with Chrome but they work with FireFox/Explorer. I don´t know why!Enrico
This method no longer works. Gives a 404 error. I tested in Chrome, Safari, FireFox.Hierology
"lh3.googleusercontent.com/-xxxxxxxxx/AAAAAAAAAAI/AAAAAAAADOU/…" seems to work still but the user-id only urls are still returning 404Oceania
I've updated the answer to reflect that the above method does not work as of a few days back.Norwich
@CeciliaAbadie: Can you confirm that your method still works? Tried several variations but couldn't make it work with my user id (see the answer above). Can you?Norwich
@rgoraya: But how do you calculate the xxxxxx part automatically?Norwich
I guess that has been deprecatedBlossom
G
47

UPDATE: Google stopped support for this method, that now returns a 404 (not found) error.


All this urls fetch the profile picture of a user:

https://www.google.com/s2/photos/profile/{user_id}
https://plus.google.com/s2/photos/profile/{user_id}
https://profiles.google.com/s2/photos/profile/{user_id}

They redirect to the same image url you get from Google API, an ugly link as
lh6.googleusercontent.com/-x1W2-XNKA-A/AAAAAAAAAAI/AAAAAAAAAAA/ooSNulbLz8U/photo.jpg

The simplest is to directly use like image source:

<img src="https://www.google.com/s2/photos/profile/{user_id}">

Otherwise to obtain exactly the same url of a Google API call you can read image headers,
for example in PHP:

$headers = get_headers("https://www.google.com/s2/photos/profile/{user_id}", 1);
echo "<img src=$headers[Location]>";

as described in article Fetch Google Plus Profile Picture using PHP.

Ground answered 15/5, 2012 at 11:15 Comment(6)
No need to make a HTTP request for each request, see my answer.Norwich
Is this documented anywhere?Proscription
Although this works perfectly, I would not go this way, as google might simply stop supporting this, since I don't see anywhere an official documentationLodging
I have added a full proof answer belowFiddlededee
Below I have added a new solution to get Google profile image by user id ,which is not returns 404 error please check that.Dean
but how to find user_id from gmail addressPhotography
M
26

Approach 1: (no longer works)

https://plus.google.com/s2/photos/profile/<user_id>?sz=<your_desired_size>

Approach 2: (each request counts in your api rate limits which is 10k requests per day for free)

https://www.googleapis.com/plus/v1/people/<user_id>?fields=image&key={YOUR_API_KEY}

with the following response format:

{ "image": { "url": "lh5.googleusercontent.com/-keLR5zGxWOg/.../photo.jpg?sz=50"; } }

Approach 3: (donot require api key)

http://picasaweb.google.com/data/entry/api/user/<user_id>?alt=json

in the json response you get a property named "gphoto$thumbnail", which contains the profile picture url like the following:

http://lh6.ggpht.com/-btLsReiDeF0/AAAAAAAAAAI/AAAAAAAAAAA/GXBpycNk984/s64-c/filename.jpg

You may notice in the url the portion "s64-c" which means the image size to be 64, I've tried using other values like "s100-c" and they worked. Also if you remove the "s64-c" part and append the "?sz=100" parameter, that will also work as of now. Though this is not very good way of getting the profile picture of a gplus user, but the advantage is it do not require any api key.

Melantha answered 24/3, 2014 at 19:40 Comment(7)
On Approach2 have you figured out how to set image size by API call?Roundtheclock
@Roundtheclock you should get the api response like this: { "image": { "url": "lh5.googleusercontent.com/-keLR5zGxWOg/.../photo.jpg?sz=50" } } from where you can set the sz parameter for different image sizesMelantha
lol..google is going to have to hack every publicly available image link to come under their API for people to have no choice but to get an API key.Intussusception
I wonder if there is something like picasaweb but you get the picture directly from the email and not google +. I just have a case of a user that has an image in your Gmail account but do not have a profile on google +, and does not bring image. Only can get an url like this lh3.googleusercontent.com/-k10Va1BdzL8/AAAAAAAAAAA/AAAAAAAAAAA/…Actinomorphic
picasaweb.google.com api is depriciated and will be turned off by Jan 2019 developers.google.com/picasa-webOd
@Od do you know of any other alternative then?Rodenticide
@Actinomorphic did you ever figure it out?Rodenticide
M
11

Google, no API needed:

$data = file_get_contents('http://picasaweb.google.com/data/entry/api/user/<USER_ID>?alt=json');
$d = json_decode($data);
$avatar = $d->{'entry'}->{'gphoto$thumbnail'}->{'$t'};

// Outputs example: https://lh3.googleusercontent.com/-2N6fRg5OFbM/AAAAAAAAAAI/AAAAAAAAADE/2-RmpExH6iU/s64-c/photo.jpg

CHANGE: the 64 in "s64" for the size

Maloney answered 3/4, 2014 at 20:7 Comment(1)
This is fine, but does not return if the avatar is the default or a real user uploaded one.Isherwood
S
4

If you want to show the profile picture for the currently logged in user, you do not even need to know the {user_id}. Simply using https://plus.google.com/s2/photos/profile/me will be enough.

Salesgirl answered 13/1, 2013 at 15:20 Comment(2)
thanks, but unfortunately it's not for current logged in userHaeres
The question was about getting the profile picture using the user_id. Not with the meCarnarvon
F
4

2020 Solution Please comment if no longer available.

In order to get profile URL from authenticated user.

GET https://people.googleapis.com/v1/people/[THE_USER_ID_OF_THE_AUTHENTICATED_USER]?personFields=photos&key=[YOUR_API_KEY] HTTP/1.1

Authorization: Bearer [YOUR_ACCESS_TOKEN]
Accept: application/json

Response:

{
  "resourceName": "people/[THE_USER_ID_OF_THE_AUTHENTICATED_USER]",
  "etag": "12345",
  "photos": [
    {
      "metadata": {
        "primary": true,
        "source": {
          "type": "PROFILE",
          "id": "[THE_USER_ID_OF_THE_AUTHENTICATED_USER]"
        }
      },
      "url": "https://lh3.googleusercontent.com/a-/blablabla=s100"
    }
  ]
}

and the link can be used as:

<img src="https://lh3.googleusercontent.com/a-/blablabla=s100">
Flare answered 26/9, 2020 at 6:12 Comment(10)
it's not as straight forward as the way facebook lets to use thoBelter
Well, it's right, but take consideration for Facebook API GRAPH 8.0 are several issues, you can't reference the image, only download it as force or broken image. I had a question about it without response yet :(Flare
@PooyaEstakhri not this case in this answer but if you are working with ASP.NET Core 3.1 you can get the image from the picture claim, is what I finally did.Flare
in case of facebook you can use <img> tag, no need to download it. unfortunately i'm not familiar with asp.net programmingBelter
@PooyaEstakhri that's exactly doesn't work with 8.0, img don't show the picture.Flare
i tested now with app scope uid of an app with v8, it works https://graph.facebook.com/1661794173890082/picture?width=200&height=200Belter
@PooyaEstakhri if you want we can discuss it in my question (has no answers yet) jsfiddle.net/t10a7voxFlare
posted my answer there.Belter
What if user changes the profile picture ? the url would show old picture, new one or nothing ?Belter
@PooyaEstakhri it changes. For any case I use a code myself to save the image in my server and update the picture if it changes.Flare
O
3

You can get the URL for the profile image using the people.get method of the Google+ API. That does require an extra round trip, but is the most reliable way to get the image.

You technically can also use the URL https://s2.googleusercontent.com/s2/photos/profile/{id}?sz={size} which will then redirect to the final URL. {id} is the Google user ID or one of the old Google Profiles usernames (they still exist for users who had them, but I don't think you can create new ones anymore). {size} is the desired size of the photo in pixels. I'm almost certain this is not a documented, supported feature, so I wouldn't rely on it for anything important as it may go away at anytime without notice. But for quick prototypes or small one-off applications, it may be sufficient.

Overturf answered 15/2, 2012 at 16:29 Comment(2)
Is this documented anywhere?Proscription
yep, Justin seems to be right... this no longer works.Overturf
F
2

trying to access the /s2/profile/photo url works for most users but not all.

The only full proof method is to use the Google+ API. You don't need user authentication to request public profile data so it's a rather simple method:

  1. Get a Google+ API key on https://cloud.google.com/console

  2. Make a simple GET request to: https://www.googleapis.com/plus/v1/people/+< username >?key=

Note the + before the username. If you use user ids instead (the long string of digits), you don't need the +

  1. you will get a very comprehensive JSON representation of the profile data which includes: "image":{"url": "https://lh4.googleusercontent.com/..... the rest of the picture url...."}
Fiddlededee answered 10/12, 2013 at 17:33 Comment(0)
C
2

If you use Flutter, then you can access it via people.googleapis.com endpoint, code uses google_sign_in library

import 'package:google_sign_in/google_sign_in.dart';

Future<String> getPhotoUrl(GoogleSignInAccount account, String userId) async {
  // final authentication = await account.authentication;
  final url = 'https://people.googleapis.com/v1/people/${userId}?personFields=photos';
  final response = await http.get(
    url,
    headers: await account.authHeaders
  );

  final data = json.decode(response.body);
  return data['photos'].first['url'];
}

You will get something like

{
  resourceName: people/998812322529259873423, 
  etag: %EgQBAzcabcQBAgUH, 
  photos: [{metadata: {primary: true, source: {type: PROFILE, id: 107721622529987673423}}, 
  url: https://lh3.googleusercontent.com/a-/abcdefmB2p1VWxLsNT9WSV0yqwuwo6o2Ba21sh_ra7CnrZ=s100}]
}

where url is an accessible image url.

Coming answered 12/3, 2019 at 14:29 Comment(0)
A
1

Tried everything possible.. here is final piece of working code. Hope it helps someone who is looking for it.

    <?
$url='https://www.googleapis.com/plus/v1/people/116599978027440206136?fields=image%2Furl&key=MY_API_KEY&fields=image';
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_PROXYPORT, 3128);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
$response = curl_exec($ch);
curl_close($ch);
$d = json_decode($response);
$avatar_url = $d->{'image'}->{'url'};
echo $avatar_url;
?>
Arundel answered 15/6, 2015 at 11:18 Comment(0)
F
-4

Simple answer: No

You will have to query the person API and the take the profile image.url data to get the photo. AFAIK there is no default format for that url that contains the userID.

Frazzle answered 3/2, 2012 at 12:36 Comment(4)
You're sure? I wonder if they have any plans to make such thing? Thanks anyway.Haeres
If you look at different profiles and look at the html output e.g. with "inspect element" in chrome you will see that they are all different and don't contain the userID. Also there is nothing on the official docs.Frazzle
It IS possible, both to get the image, and set a specific size to look for. See my answer below.Norwich
It was not possible when the question and this answer where posted :)Haeres

© 2022 - 2024 — McMap. All rights reserved.