2012-03-20 170 views
5

我仔细阅读了谷歌通讯API文档,但我无法获得正确的PUT请求(即更新)。我正在使用Ruby on Rails 3.2和OAuth gem(v0.4.5)。我取得与Omniauth令牌和范围定义为“https://www.google.com/m8/feeds谷歌联系人API:未经授权的401未知授权标头

让我证明:

ruby-1.9.2-p290 :001 > @access_token.get("https://www.google.com/m8/feeds/contacts/default/full/c1f86b48b52548c", {"GData-Version" => "3.0"}) 
=> #<Net::HTTPOK 200 OK readbody=true> 

正如你可以看到,GET请求很好的工作,所以我的OAuth访问令牌应该是好的。删除请求的工作也是:

ruby-1.9.2-p290 :002 > @access_token.delete('https://www.google.com/m8/feeds/contacts/default/full/c1f86b48b52548c', { 'GData-Version' => '3.0', 'If-Match' => '"RH46fTVSLyt7I2A9Wx9VFkgMQAU."' }) 
=> #<Net::HTTPOK 200 OK readbody=true> 

到目前为止,这么好。在这里,我按照Google的文档[1]的说明在请求标题中提供了联系人条目的Etag。所以它不应该造成任何麻烦。

按照OAuth的宝石文档[2]的PUT请求的语法应该像以下:

- (Object) put(path, body = '', headers = {}) 

而从文档的例子:

@token.put('/people/123', @person.to_xml, { 'Accept' => 'application/xml', 'Content-Type' => 'application/xml' }) 

据我明白,我应该发送一个简单的XML字符串作为PUT请求的主体。所以,让我们先取一些示例数据:

ruby-1.9.2-p290 :003 > xmldata = @access_token.get("https://www.google.com/m8/feeds/contacts/default/full/5f74bf0d5c621a", {"GData-Version" => "3.0"}).body 

=> "<?xml version='1.0' encoding='UTF-8'?> 
     <entry xmlns='http://www.w3.org/2005/Atom' xmlns:gContact='http://schemas.google.com/contact/2008' xmlns:batch='http://schemas.google.com/gdata/batch' xmlns:gd='http://schemas.google.com/g/2005' gd:etag='&quot;R3w4fzVSLit7I2A9WhRaGEQCQgc.&quot;'> 
     <id>http://www.google.com/m8/feeds/contacts/my.email%40address.com/base/5f74bf0d5c621a</id> 
     <updated>2012-02-22T08:15:36.237Z</updated> 
     <app:edited xmlns:app='http://www.w3.org/2007/app'>2012-02-22T08:15:36.237Z</app:edited> 
     <category scheme='http://schemas.google.com/g/2005#kind' term='http://schemas.google.com/contact/2008#contact'/> 
     <title>Joe Average</title> 
     <link rel='http://schemas.google.com/contacts/2008/rel#photo' type='image/*' href='https://www.google.com/m8/feeds/photos/media/my.email%40address.com/5f74bf0d5c621a?v=3.0'/> 
     <link rel='self' type='application/atom+xml' href='https://www.google.com/m8/feeds/contacts/my.email%40address.com/full/5f74bf0d5c621a?v=3.0'/> 
     <link rel='edit' type='application/atom+xml' href='https://www.google.com/m8/feeds/contacts/my.email%40address.com/full/5f74bf0d5c621a?v=3.0'/> 
     <gd:name> 
      <gd:fullName>Joe Average</gd:fullName> 
      <gd:givenName>Joe</gd:givenName> 
      <gd:familyName>Average</gd:familyName> 
     </gd:name> 
     <gd:email rel='http://schemas.google.com/g/2005#other' address='[email protected]' primary='true'/> 
     </entry>" 

...并尝试更新回谷歌:

ruby-1.9.2-p290 :004 > @access_token.put("https://www.google.com/m8/feeds/contacts/default/full/5f74bf0d5c621a", xmldata, {"GData-Version" => "3.0", 'If-Match' => '"R3w4fzVSLit7I2A9WhRaGEQCQgc."'}) 

=> #<Net::HTTPUnauthorized 401 Unknown authorization header readbody=true> 

所以这是行不通的。让我们尝试剥离下来的XML一点,所以它会看起来就像在文档[1]:

xmldata = "<entry gd:etag='&quot;R3w4fzVSLit7I2A9WhRaGEQCQgc.&quot;'> 
      <id>http://www.google.com/m8/feeds/contacts/my.email%40address.com/base/5f74bf0d5c621a</id> 
      <updated>2012-02-22T08:15:36.237Z</updated> 
      <category scheme='http://schemas.google.com/g/2005#kind' term='http://schemas.google.com/contact/2008#contact'/> 
      <link rel='http://schemas.google.com/contacts/2008/rel#photo' type='image/*' href='https://www.google.com/m8/feeds/photos/media/my.email%40address.com/5f74bf0d5c621a?v=3.0'/> 
      <link rel='self' type='application/atom+xml' href='https://www.google.com/m8/feeds/contacts/my.email%40address.com/full/5f74bf0d5c621a?v=3.0'/> 
      <link rel='edit' type='application/atom+xml' href='https://www.google.com/m8/feeds/contacts/my.email%40address.com/full/5f74bf0d5c621a?v=3.0'/> 
      <gd:name> 
       <gd:fullName>Joe Average</gd:fullName> 
       <gd:givenName>Joe</gd:givenName> 
       <gd:familyName>Average</gd:familyName> 
      </gd:name> 
      <gd:email rel='http://schemas.google.com/g/2005#other' address='[email protected]' primary='true'/> 
      </entry>" 

,但没有运气,完全同样的错误了。

这是我现在完全卡住的地方。我会非常感谢任何提示正确的方向。

[1] https://developers.google.com/google-apps/contacts/v3/#updating_contacts

[2] http://rubydoc.info/gems/oauth/0.4.5/OAuth/AccessToken

+0

您可以尝试在文件中格式化请求,然后使用'curl'发出请求,以查看是否可以排除某些可能发生的魔法。弹出的一个可能性是有些东西可能已被编码(例如'"'),这不应该是。 – 2012-03-20 00:59:16

回答

0

尝试在PUT请求设置你的Content-Type头到 '应用程序/原子+ XML'。我遇到了类似的问题,这样做有所帮助。