Retrieving a user's public google/gmail pictur

2019-01-08 10:29发布

I'm writing an email-based application using the gmail-api, and i need to retrieve user public images for email address originating from google. I've seen other applications do that, but i can't seem a way to reliably get this data.

some more information:

  • i'm using oauth2 to sign the user in - and i have no problem requesting any type of permissions there.
  • i've tried retrieving the image using the google+ api - but it requires a userID for that - and i don't know how to get the userId for the email address (gmail-api doesn't give it, afaik).
  • i've tried using the contacts API - but it only gives images for the user's contacts, while the email clients can display images from other users as well.

Thanks!

Edit:

I know i can receive the required images from google plus, if i have the other user's userid. however, i can't find a way to get the userid based on an email address.

8条回答
啃猪蹄的小仙女
2楼-- · 2019-01-08 10:30

Based on @jose920405 answer, I have written this free public API service called Pikmail API. Here some advantages.

  • No authentication needed.
  • It's totally free and open source.
  • It's written in Kotlin.
  • You don't need to write some wrapper for mapping the response.

Here is the blog post about how Pikmail API works internally.

Usage:

Html

<img src="https://pikmail.herokuapp.com/eduardo.alejandro.pool.ake@gmail.com?size=50" alt="Profile Picture">

Profile Picture

Java Android

Picasso.with(context).load("https://pikmail.herokuapp.com/eduardo.alejandro.pool.ake@gmail.com?size=50").into(imageView);

Feel free to contribute with you pull requests, open bugs or request new features here.

Note: This service is based on an independent library called Pikmail that can be used as a Gradle dependency in your java projects(servers or Android apps).

查看更多
手持菜刀,她持情操
3楼-- · 2019-01-08 10:38

Google+ is the right way to do this. This actually has nothing to do with the Gmail API. You don't need a the user's userid, just a valid access token with appropriate scopes.

https://www.googleapis.com/plus/v1/people/me?access_token=YOUR_ACCESS_TOKEN_HERE

Unless there is an error/invalid token/etc, this will return a bunch of information about the user as JSON. Included in this data will be a URI for the user's profile image.

EDIT:

To get the profile picture for a user based on the email address, first search for the user

https://www.google.com/m8/feeds/contacts/default/thin?q=EMAIL_ADDRESS_HERE

You can then retrieve the correct userID for that user from the returned JSON, and use it to get a profile image.

查看更多
Melony?
4楼-- · 2019-01-08 10:39

If you are authenticating users in your app with Google using the OAuth 2.0 API -For example using passport -> https://www.npmjs.com/package/passport-google-oauth20

You can request the "profile" scope, which gives you a bunch of information from displayName to the users profile photos "profile.photos[0]"

查看更多
地球回转人心会变
5楼-- · 2019-01-08 10:42

You can use google app profile API

here is the sample code to access profile pic

function getProfilePic(userName){
  userName = 'yourusername@myDomain.com'; 
  var scope = 'https://www.google.com/m8/feeds/';
  var fetchArgs = googleOAuth_('Profile', scope);
  fetchArgs.method = 'GET';
  var domain = UserManager.getDomain();
  var url = 'https://www.google.com/m8/feeds/photos/profile/'+domain+'/'+userName+'?v=3';
  var rawData = UrlFetchApp.fetch(url, fetchArgs).getContentText();

 //here you get data into rawData variable

}

//google oAuth
function googleOAuth_(name,scope) {
  var oAuthConfig = UrlFetchApp.addOAuthService(name);
  oAuthConfig.setRequestTokenUrl("https://www.google.com/accounts/OAuthGetRequestToken?scope="+scope);
  oAuthConfig.setAuthorizationUrl("https://www.google.com/accounts/OAuthAuthorizeToken");
  oAuthConfig.setAccessTokenUrl("https://www.google.com/accounts/OAuthGetAccessToken");
  oAuthConfig.setConsumerKey("anonymous");
  oAuthConfig.setConsumerSecret("anonymous");
  return {oAuthServiceName:name, oAuthUseToken:"always"};
}

you can get more information here

查看更多
干净又极端
6楼-- · 2019-01-08 10:45

a simple javascript to fetch public profile image from google:

<html>
<body>


<script type="text/javascript">
function getPhoto() {

    var xmlhttp = new XMLHttpRequest();
    xmlhttp.onreadystatechange = function() {
    if (this.readyState == 4 && this.status == 200) {
        myArr = JSON.parse(this.responseText);
        try { 
            profilePic = myArr["feed"]["entry"][0]["media$group"]["media$thumbnail"][0]["url"]; 
            document.getElementById("demo").innerHTML="<img src='" + profilePic + "' width='160px' height='160px'>"
            }
        catch(err) { }

        }
    }
xmlhttp.open("GET", "https://picasaweb.google.com/data/feed/api/user/<EMAIL>?kind=album&alt=json", true);
xmlhttp.send();
}

</script>

<div id="demo" onclick="javascript:getPhoto();">click here to load photo</div>

</body>
</html>
查看更多
Melony?
7楼-- · 2019-01-08 10:51

Is very easy

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

just has a little problem.

You only can get the picture of your Google+ profile, not directly from your gmail.

UPDATE:

This API is being deprecated and will be turned down in January 2019. Migrate to Google Photos Library API as soon as possible to avoid disruptions to your application.

Info Here

查看更多
登录 后发表回答