Search code examples
pythongoogle-apigdata

Google contacts. GetPhoto


We want to import contacts photos from google API.

Following code was working for me.

for email, name, entry in feed:
    photo = client.GetPhoto(entry)
    if photo:
        fname = str(uuid.uuid4()) + '.jpg'
        image_file = open(dname + '/' + fname, 'wb')
        image_file.write(photo)
        image_file.close()
        result[email] = '/media/import/%s/%s' % (dir, fname)

Now, for some reasons, we get in files atom feed copy. So method GetPhoto no working.

Any ideas, why it happened and what is current way to retrieve contacts photo?


Solution

  • Here is work around google API changes. Now we're using direct requests to API.

    for email, name, entry in feed:
        photo_link = e.GetPhotoLink()
        if photo_link:
            request = http.request(
                'GET',
                photo_link.href,
                headers={
                    'Authorization':'OAuth %s' % client.current_token.access_token
                }
            )
    
            if request.status == 200:
                photo = str(request.read())
                fname = str(uuid.uuid4()) + '.jpg'
                image_file = open(dname + '/' + fname, 'wb')
                image_file.write(photo)
                image_file.close()
                result[email] = '/media/import/%s/%s' % (tid, fname) #str(photo.href) #