13

I'm creating an email-based application in React with the Gmail API. I'm loading in a bunch of messages, and I want to display avatars of the senders.

I'm using this package to display avatars. I'd like to either have a Google Id or an image url. It would also be nice to be able to get images for other popular services, such as Outlook and Yahoo mail.

Other people have asked this same question, but all the answers either seem to use the Picasa API (which is deprecated) or the Google+ API (which will be deprecated).

EDIT: Yes, the react-avatar package claims it can find an avatar based on supplied user data, but all it does with the email is find a Gravatar, which many people don't have.

Linda Lawton - DaImTo
  • 106,405
  • 32
  • 180
  • 449
kognise
  • 612
  • 1
  • 8
  • 23
  • Maybe you can use Firebase Authentication? – NoobTW Feb 10 '19 at 01:40
  • 1
    @NoobTW No thank you. I'd rather stick with the plain old Gmail API, especially since we've built a lot based on it. Also, does Firebase Auth even support Gmail APIs? I've always thought it's just for login. – kognise Feb 10 '19 at 01:45
  • Will this not get you what you want: https://developers.google.com/admin-sdk/directory/v1/reference/users/photos/get – Lance Whatley Feb 11 '19 at 22:37
  • 1
    @LanceWhatley I believe that api is limited to searching the users of a particular G Suite domain – cody Feb 12 '19 at 00:29
  • @cody Yeah, I think so. – kognise Feb 12 '19 at 19:15
  • It used to be available via the Google+ API, but it was shutdown due to security see: https://www.blog.google/technology/safety-security/project-strobe/ There isn't an anonymous way – David Bray Feb 14 '19 at 22:05

4 Answers4

12

What you want to do is not possible via the GMAIL API or any other Google API

You can not take a Google email address and search for the image or any other personal profile information associated with that email address via any Google API endpoint. Explanation below.

Get image of the sender of an email

Gmail API lists the email messages that have been sent to and by a user. The gmail API is basically returning the mail server email message response in MIME format which if you check does not contain a profile image. It does not return to you the image of a the sender or the reciever.

Google does not have endpoint for developers to use to searching on a gmail.com email address and returning any user profile information (including image) this would be against the users privacy. A user would have to grant you permission to see their image and you dont have that permission for every gmail user who may be sending emails to your authenticated user.

The gmail website probably does some kind of check on gmail email addresses and puts the picture on attached with the account. Google has access to the profile data of all GMAIL users, google cant give you this same access as it would be against the users privacy. If its not a gmail account they may check Gravatar to see if an image has been set up for this email address. Again there is no way for you to request the image of a google user using their email address.

  • You could check Gravatar to see if one had been set up for that email
  • If the authenticated user has added this user as a contact and has added a picture for this user you may be able to use the People api.

In the past i have recommend to users that as an image they take the first letter of the users email address and create an image using that letter. You may also want to use a question mark which is actually what gmail does when its website cant find an email from the user probably by checking Gravatar.

enter image description here

Get image of current authenticated user

You can get this information from the people.get endpoint just make sure that you have requested the profile scope from the user when you authenticate them

GET https://people.googleapis.com/v1/people/me

It returns a large response containing the users profile information part of it contains the users picture

"photos": [
    {
      "url": "https://lh3.googleusercontent.com/a-/AAuE7mDuIWqXzrrp-65cIhXSD2HjCI8WYsWHR0fDx5_wQPY=s100", 
      "metadata": {
        "source": {
          "type": "PROFILE", 
          "id": "117200475532672775346"
        }, 
        "primary": true
      }
    }
  ], 

The offical sample project for people api contains information on how to connect to the api. Just make sure to add the 'profile' scope

The code to get the picture should be something like this.

function getPicture() {
        gapi.client.people.people.get({
           'resourceName': 'people/me',
           'pageSize': 10,
           'personFields': 'photos',
         }).then(function(response) {
           var connections = response.result.connections;
           appendPre('Connections:');

           if (connections.length > 0) {
             for (i = 0; i < connections.length; i++) {
               var person = connections[i];
               if (person.url && person.url.length > 0) {
                 appendPre(person.names[0].url)
               } else {
                 appendPre("No display name found for connection.");
               }
             }
           } else {
             appendPre('No connections found.');
           }
         });
      }
Linda Lawton - DaImTo
  • 106,405
  • 32
  • 180
  • 449
  • 3
    I don't want the current user's profile picture, I want to get a profile picture based on someone's email address. I'm displaying a list of emails, and I want to display their avatars. – kognise Feb 12 '19 at 19:15
  • Have you tried going through Gravatar api? Google will only give you the information on the currently authenticated user. – Linda Lawton - DaImTo Feb 13 '19 at 06:07
  • Yes, I have. It doesn't really have that many results. Alsom if so, how does Gmail itself fetch avatars? – kognise Feb 13 '19 at 23:43
  • As i stated there is no way to get the gmail AVATARS of a random user. please read the section **Get image of the sender of an email** – Linda Lawton - DaImTo Feb 14 '19 at 07:17
  • Would the -1 please comment. Explaining in detail why something is not possible is not really a valid reason for a -1 IMO – Linda Lawton - DaImTo Feb 14 '19 at 07:25
  • This isn't exactly what I was looking for, but it seems to be the only solution. Thank you for your time. – kognise Feb 18 '19 at 00:18
  • 1
    Unfortunately everything is not possible when you are dealing with user privacy. Even things that sound like a really simple thing and great idea at the time. – Linda Lawton - DaImTo Feb 18 '19 at 07:12
5

The currently accepted answer isn't entirely correct, as there absolutely is a Google API which exposes Gmail Avatars - as well as the user's first- and/or Lastname, as long as the user has configured any publically.

Although I have so far been unable to find the actual API endpoint you can use to achieve this, I did come across avatarapi.com.

As stated on their website:

This API uses public profile information provided by Google, via a publicly exposed Google data source to determine the name and profile image of the user.

They have a fallback to Gravatar. I'd also love to find out where this 'Google Data source' they use is located.

Jerre
  • 179
  • 2
  • 7
  • 5
    This doesn't work. I tried it with my Gmail address and all I got back was a silhouette image. – FractalBob Jun 15 '20 at 06:30
  • 1
    Tried it today with 4 different profiled. Today it worked partially. My own gmail and also the wordpress fallback for a diffent email worked. I also administrate a Google Workspace. I tried 5 emails 3 worked, 2 not, even if they all had pictures. I have no clue why, but you can just try on the start page with some emails. – Alexander Taubenkorb May 10 '22 at 12:30
  • I got far better results. They claim to have now more than a billion records on their database, and I've tried a few of my addresses, some of which registered with Gravatar, others not registered at all (except on the mail provider they've been registered on, e.g. Yahoo). I've got a *lot* of addresses :-) and I'd guess that about 80%-90% were correctly identified. Also, I was surprised at the sheer speed of this API! No wonder it's a paid service (no free tier — after 100 API calls as demo/test, you're starting to get billed for usage). – Gwyneth Llewelyn Mar 27 '23 at 23:38
4

I don't think that google shares that info with anyone without permission . You have to create a profile for every user and take the profile image only once when the user registers in your platform by the Google oauth Check this info https://developers.google.com/actions/identity/google-sign-in-oauth

Bledi Deda
  • 41
  • 3
2

Gravatar

It is possible that the user associated with that email address has signed up for Gravatar. In that case, you can easily obtain the Gravatar avatar.

You need to get the md5 of the email first, then

var email = "youremail@gmail.com",
    size = 80; 
yourImg.src = 'http://www.gravatar.com/avatar/' + MD5(email) + '.jpg?s=' + size;

https://jsfiddle.net/a4g8s5y2/1/

Flimm
  • 136,138
  • 45
  • 251
  • 267
fedeghe
  • 1,243
  • 13
  • 22