I have carefully read Google Contacts API documentation, but I can't get PUT requests (i.e. updates) right. I'm using Ruby on Rails 3.2 with OAuth gem (v0.4.5). I fetch tokens with Omniauth and the scope is defined as "https://www.google.com/m8/feeds"
Let me demonstrate:
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>
As you can see, GET requests work nicely so my OAuth access token should be alright. DELETE requests work, too:
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>
So far, so good. Here I provided the contact entry's Etag in the request header as instructed in Google's documentation [1]. So it shouldn't be causing any trouble.
According to the OAuth gem documentation [2] the syntax of a PUT request should be like following:
- (Object) put(path, body = '', headers = {})
And an example from the documentation:
@token.put('/people/123', @person.to_xml, { 'Accept' => 'application/xml', 'Content-Type' => 'application/xml' })
As far as I understand, I should be sending a simple XML string as the body of the PUT request. So let's fetch some example data first:
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>"
... and try to update it back to Google:
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>
So this doesn't work. Let's try to strip down the XML a little so it'll look just like in documentation [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>"
But no luck, exactly the same error as before.
This is where I'm totally stuck now. I would greatly appreciate any hints to right direction.
[1] https://developers.google.com/google-apps/contacts/v3/#updating_contacts
curl
to see if you can rule out some of the magic that may be happening. One thing that pops out as a possibility is that some things may have been encoded (e.g."
) that shouldn't be. – Tom Harrison