在 Ruby 中更新 Google 联系人图片时出现问题
我正在尝试编写一个 ruby 应用程序来更新 Google 联系人照片,但无法上传。我不确定我是否误解了 Google API,但这就是我到目前为止所得到的:
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
Google Data API 说:
每个联系人都有一个照片链接 元素。它的形式如下:
href='https://www.google.com/m8/feeds/photos/media/liz%40gmail.com/c9012de' gd:etag='"KTlcZWs1bCp7ImBBPV43VUV4LXEZCXERZAc。"'>
该元素是否出现 联系人有相关照片或 不是。如果联系人确实有照片, 那么该元素包含一个 gd:etag 指定 ETag 的属性 照片。如果联系人没有照片, 那么就没有 gd:etag 属性,并且 href 属性提供 URL 用于向联系人添加照片。
要添加或更新给定的照片 如上所示的元素,发送一个新的 使用 PUT 命令将照片发送到 URL: https://www.google.com/m8/feeds /photos/media/liz%40gmail.com/c9012de。 请记住设置正确的“image/*” 内容类型标头。
要删除照片,请发送 HTTP DELETE 请求相同的 URL。
更新或删除照片时, 在 If-Match 中发送照片的 ETag HTTP 请求的标头。 或者,使用 If-Match: * 来 更新或删除,无论是否 你有最新版本的 照片。
谁能看到我哪里出错了?
更新和回答: 在 StackOverflow 和一位伙伴的帮助下,我成功地完成了这项工作。基本上,错误是设置了不正确的 MIME 类型(见下文),而且我没有通过 Google 正确验证 PUT 请求。我试图编写一个脚本,用来自 Gravatar 的图像(如果可能)更新任何丢失的 Google 联系人照片。一旦我让它工作起来,效果就很好:成功匹配了大约 2 - 3 打联系人。我写了一篇关于该脚本的博客文章,以防有人也想运行它:http://ashleyangell.com/2011/06/ruby-script-to-import-google-contact-photos-from-gravatar/
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/
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我自己从未使用过 Google Data API,但尝试将 Content-Type 标头设置为
image/jpeg
,而不仅仅是image/*
。I've never used the Google Data API myself, but try setting the Content-Type header to, for example,
image/jpeg
, rather than justimage/*
.