我仔細閱讀了谷歌通訊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='"R3w4fzVSLit7I2A9WhRaGEQCQgc."'>
<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='"R3w4fzVSLit7I2A9WhRaGEQCQgc."'>
<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
您可以嘗試在文件中格式化請求,然後使用'curl'發出請求,以查看是否可以排除某些可能發生的魔法。彈出的一個可能性是有些東西可能已被編碼(例如'"'),這不應該是。 – 2012-03-20 00:59:16