在Ruby中更新Google联系人图片时出现问题 [英] Problem updating Google Contact Picture in Ruby

查看:222
本文介绍了在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 / *'})
结束

Google Data API说:


每个联系人都有一个图片链接
元素。它的形式如下:




href ='https://www.google.com/m8/feeds/photos/media/liz%40gmail.com / c9012de'
gd:etag ='KTlcZWs1bCp7ImBBPV43VUV4LXEZCXERZAc。'>



显示
联系人是否有关联照片或
不是。如果联系人确实有照片
,那么该元素包含为
照片指定ETag的gd:etag
属性。如果联系人没有照片,
则没有gd:etag属性,
href属性提供的URL为
用于向联系人添加照片。



要添加或更新给定上述
元素的照片,请发送一个带有PUT命令的新
照片到URL:
https://www.google.com/m8/feeds/photos/media/利兹%40gmail.com / c9012de
记得设置一个合适的image / *
Content-Type标题。



要删除一张照片,发送一个HTTP DELETE

$ b 在更新或删除照片时,
将照片的ETag发送到HTTP的If-Match
标头中请求。
或者,使用If-Match:*以
更新或删除,无论
是否有最新版本的
照片。


blockquote>

任何人都可以看到我出错的地方吗?

更新和回答:
我设法从StackOverflow和队友那里得到这个工作。基本上错误的是设置了不正确的MIME类型(见下文),但我也没有通过Google针对PUT请求进行正确的身份验证。我试图写的是一个脚本,它用来自Gravatar的图像(如果可能)更新任何失踪的Google联系人照片。一旦我得到它的工作,它运作得很好:设法匹配大约2-3个联系人。我在脚本上写了一篇博文,以防有人想要运行它: http://ashleyangell.com/2011/06/ruby-script-to-import-google-contact-photos-from-gravatar/

解决方案

我自己从未使用Google Data API,但尝试将Content-Type标头设置为,例如 image / jpeg ,而不仅仅是 image / *


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/

解决方案

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/*.

这篇关于在Ruby中更新Google联系人图片时出现问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆