检索Google用户照片 [英] Retrieving Google User Photo

查看:89
本文介绍了检索Google用户照片的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我能够从google admin SDK的user.list api中检索thumbnailPhotoUrl.但是,每当我尝试渲染图像时,Google都会重定向到静态轮廓图像.通过API检索的URL看起来像

I am able to retrieve the thumbnailPhotoUrl from the user.list api of the google admin SDK. However, whenever I try to render the image, Google is redirecting to a static silhouette image. The URL that is retrieved via the API looks like

https://plus.google.com/_/focus/photos/private/AIbEiAIA.... 

如前所述,最终将重定向到:

As mentioned, this ends up getting redirected to:

https://ssl.gstatic.com/s2/profiles/images/silhouette200.png

但是,通过一些逆向工程,我可以通过在URL路径的开头添加/u/1/来查看照片,如下所示:

However, with a little bit of reverse engineering, I can see the photo by adding /u/1/ to the beginning of the URL path, like this:

https://plus.google.com/u/1/_/focus/photos/private/AIbEiAIA...

根据我的研究,/u/1与多个Google帐户有关,因此,恐怕我将无法依靠这种方法.谁能帮助我了解这里的情况?

From my research the /u/1 has something to do with multiple google accounts, so I'm afraid I wouldn't be able to rely on this method. Can anyone help me understand what's happening here?

推荐答案

根据我的经验,我发现如果 thumbnailPhotoUrl 在URL上具有私密性,则该照片不是公开的,即不可见在网域外,也可能是用户尚未激活他们的Google+个人资料,我认为无论如何都会公开其照片.

Out of my own experience I figured out that if the thumbnailPhotoUrl has private on the URL then the photo is not public i.e. not viewable outside the domain, it could also be that the user hasn't activated their Google+ profile, which I believe makes their photo public anyway.

如果URL上有私有路径,则最好避免使用 thumbnailPhotoUrl .我认为使用

Best to avoid using the thumbnailPhotoUrl if the URL has a private path on it. I think it's more reliable to retrieve the photo as Web-safe base64 data using the Users.Photo API then encode it as an inline base64 CSS image.

这是我通常使用的代码段:

This is the code snippet I usually use:

    import com.google.common.io.BaseEncoding;
    import com.google.api.services.admin.directory.model.User;
    import com.google.api.services.admin.directory.model.UserPhoto;

public class PhotoUtils {

    public void loadPhoto() {
            // if the user have not signed up for Google+ yet then their thumbnail is private
            String thumbnailUrl = user.getThumbnailPhotoUrl();
            String photoData = "";
            if((thumbnailUrl != null) && (thumbnailUrl.indexOf("private") > -1)) {

                    UserPhoto photo = getUserPhoto(user.getId());
                    if(photo != null) {
                        photoData = getBase64CssImage(photo.getPhotoData(), photo.getMimeType());
                    }
            }

    }

        public static String getBase64CssImage(String urlSafeBase64Data, String mimeType) {
            urlSafeBase64Data = new String(BaseEncoding.base64().encode(
                      BaseEncoding.base64Url().decode(urlSafeBase64Data)));
            return "data:" + mimeType + ";base64," + urlSafeBase64Data;
        }

         public UserPhoto getUserPhoto(String userId) throws IOException {
            UserPhoto photo = null;
            try {
                photo = this.getClient().users().photos().get(userId).execute();

            } catch(GoogleJsonResponseException e) {
                if(e.getMessage().indexOf(NOT_FOUND) == 0) {
                    log.warning("No photo is found for user: " + userId);

                } else {
                    throw e;
                }
            }
            return photo;
        }
}

这篇关于检索Google用户照片的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆