Google API : Getting a Contacts Photo

后端 未结 4 1943
闹比i
闹比i 2021-02-14 09:02

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 Goog

相关标签:
4条回答
  • 2021-02-14 09:21

    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

    0 讨论(0)
  • 2021-02-14 09:32

    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 讨论(0)
  • 2021-02-14 09:34

    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

    0 讨论(0)
  • 2021-02-14 09:38

    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

    0 讨论(0)
提交回复
热议问题