检索 Google 用户照片

Dou*_*son 8 google-admin-sdk

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

https://plus.google.com/_/focus/photos/private/AIbEiAIA.... 
Run Code Online (Sandbox Code Playgroud)

如前所述,这最终会被重定向到:

https://ssl.gstatic.com/s2/profiles/images/silhouette200.png
Run Code Online (Sandbox Code Playgroud)

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

https://plus.google.com/u/1/_/focus/photos/private/AIbEiAIA...
Run Code Online (Sandbox Code Playgroud)

根据我的研究, /u/1 与多个谷歌帐户有关,所以我恐怕无法依赖这种方法。谁能帮我理解这里发生了什么?

ome*_*rio 5

根据我自己的经验,我发现如果thumbnailPhotoUrl在 URL 上是私有的,那么照片就不是公开的,即在域外无法查看,也可能是用户没有激活他们的 Google+ 个人资料,我相信这会让他们照片公开反正。

如果 URL 上有私有路径,最好避免使用thumbPhotoUrl。我认为使用Users.Photo API将照片作为 Web 安全 base64 数据检索然后将其编码为内联 base64 CSS 图像更可靠。

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

    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;
        }
}
Run Code Online (Sandbox Code Playgroud)