Google API : Getting a Contacts Photo

五迷三道 提交于 2019-12-03 12:12:20

You would need to make a request to the url, but also include the access_token as a query parameter.

So, using your example, let's say if your access_token is ABCDEF123456ABCDEF, then the GET request you want to make is:

https://www.google.com/m8/feeds/photos/media/liz%40gmail.com/c9012de?access_token=ABCDEF123456ABCDEF

Just a small hint, according to Google's API docs:

Note: If a contact does not have a photo, then the photo link element has no gd:etag attribute.

More info here

First, you need to make a authorized GET to that url, i.e., in the Authorization header you have to put "OAuth " + AccessToken. Also, I haven't tried but, as Savil has said, with the Access Token as a Query Parameter, you can also achieve the same.

In any case, Google responds you with the bytes of the image, so you cannot display as is. You'll need either save the byte array to a file in your server (I don't think this to be a good solution) or find another way to display the photo

If you want to read more about it, here is Google's documentation about contact photos

This is a rather old question, but nevertheless I hope this can be helpful

Use the same authorized request code used for retrieving contacts and just replace the url with the link rel url of the contact image. Response will be the bytes of the image. Use the following code to return image as response.

        //'in' is the inputStream returning from the call, response is the HttpServletResponse
        ByteArrayOutputStream out = new ByteArrayOutputStream();
        byte[] buffer = new byte[4096];
        int read;
        while (true) {
             if ((read = in.read(buffer)) != -1) {
                out.write(buffer, 0, read);
            } else {
                break;
            }
        }
        response.setContentType("image/jpeg");
        response.setContentLength(buffer.length);
        request.getSession().setAttribute("image", new String(out.toByteArray()));
        response.getOutputStream().write(buffer); 
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!