开发者

Google API : Getting a Contacts Photo

开发者 https://www.devze.com 2023-03-05 18:09 出处:网络
I\'ve been able to retrieve everything but the contacts photo by following the API. I can get the img url as well as the gd:etag from the xml returned. Below is the Google API example, and it is the

I've been able to retrieve everything but the contacts photo by following the API.

I can get the img url as well as the gd:etag from the xml returned. Below is the Google API example, and it is the same thing I get, with the value of the attributes being different of course for my contacts.

    <link rel='http://schemas.google.com/contacts/2008/rel#photo' type='image/*'
  href='https://www.google.com/m8/feeds/photos/media/liz%40gmail.com/c9012de'
  gd:etag='"KTlcZWs1bCp7ImBBPV43VUV4LXEZCXERZAc."'>

The problem is I don't know how to get it to display. When I try it, I just get the last part of the url (ie: "/32432eewqdweq") and no image.

I'm using rails, and this is my second week of doing web development, sorry if I seem noo开发者_如何学Cbish aha.

Any help would be appreciated!

Thanks,

Goran


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); 
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号