问题
I am trying to write a ruby app to update Google Contact Photos but I cant get the upload working. Im not sure if I have misunderstood the Google API but this is what I have so far:
Net::HTTP.start(URI.parse(image_element.attributes['href']).host) do |http|
response, body = http.put(URI.parse(image_element.attributes['href']).path, data, {'If-Match' => '*', 'content-type'=>'image/*'})
end
The Google Data API says:
Every contact has a photo link element. It has the form:
href='https://www.google.com/m8/feeds/photos/media/liz%40gmail.com/c9012de' gd:etag='"KTlcZWs1bCp7ImBBPV43VUV4LXEZCXERZAc."'>
That element appears whether the contact has an associated photo or not. If the contact does have a photo, then the element contains a gd:etag attribute specifying an ETag for the photo. If the contact has no photo, then there's no gd:etag attribute, and the href attribute provides the URL to use to add a photo to the contact.
To add or update a photo given the element shown above, send a new photo with a PUT command to the URL: https://www.google.com/m8/feeds/photos/media/liz%40gmail.com/c9012de. Remember to set a proper "image/*" Content-Type header.
To delete a photo, send an HTTP DELETE request to the same URL.
When updating or deleting a photo, send the photo's ETag in the If-Match header of the HTTP request. Alternatively, use If-Match: * to update or delete regardless of whether you have the latest version of the photo.
Can anyone see where I am going wrong?
UPDATE AND ANSWER: I've managed to get this working with some help from StackOverflow and a mate. Basically what was wrong was the incorrect MIME type being set (see below) but also I wasn't authenticated properly with Google for the PUT request. What I was trying to write was a Script that updates any missing Google Contact Photos with images from Gravatar (where possible). Once I got it working it worked great: managed to match about 2 - 3 dozen contacts. I wrote a blog post on the script in case anyone wants to run it also: http://ashleyangell.com/2011/06/ruby-script-to-import-google-contact-photos-from-gravatar/
回答1:
I've never used the Google Data API myself, but try setting the Content-Type header to, for example, image/jpeg
, rather than just image/*
.
来源:https://stackoverflow.com/questions/6151034/problem-updating-google-contact-picture-in-ruby