Google API : Getting a Contacts Photo

南笙酒味 提交于 2020-01-22 13:55:06

问题


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 noobish aha.

Any help would be appreciated!

Thanks,

Goran


回答1:


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




回答2:


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




回答3:


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




回答4:


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


来源:https://stackoverflow.com/questions/5983484/google-api-getting-a-contacts-photo

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!