5

Google Contacts API のドキュメントを注意深く読みましたが、PUT リクエスト (更新など) を正しく取得できません。OAuth gem (v0.4.5) で Ruby on Rails 3.2 を使用しています。私は Omniauth でトークンを取得し、スコープは " https://www.google.com/m8/feeds "として定義されています

実演してみましょう:

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 アクセス トークンは問題ないはずです。DELETE リクエストも機能します。

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 gem ドキュメント [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='joe.average@isp.net' primary='true'/>
      </entry>"

...そして、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>

したがって、これは機能しません。ドキュメント [1] のようになるように、XML を少し削除してみましょう。

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='joe.average@isp.net' 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

4

2 に答える 2

0

PUT リクエストで Content-Type ヘッダーを「application/atom+xml」に設定してみてください。私は同様の問題を抱えていて、それが役に立ちました。

于 2013-05-11T23:41:52.597 に答える