【问题标题】:Google Contacts API: Unauthorized 401 Unknown authorization headerGoogle Contacts API:未经授权的 401 未知授权标头
【发布时间】:2012-04-04 12:07:10
【问题描述】:

我已仔细阅读 Google 联系人 API 文档,但无法正确获取 PUT 请求(即更新)。我正在使用 Ruby on Rails 3.2 和 OAuth gem (v0.4.5)。我使用 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>

所以这行不通。让我们尝试对 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='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

【问题讨论】:

  • 您可以尝试在文件中格式化请求,然后使用curl 发出请求,看看您是否可以排除一些可能发生的魔法。可能出现的一件事是,某些内容可能已被编码(例如&amp;quot;),而这是不应该的。

标签: xml ruby-on-rails-3 oauth google-api google-contacts-api


【解决方案1】:

尝试在 PUT 请求中将 Content-Type 标头设置为“application/atom+xml”。我遇到了类似的问题,这样做很有帮助。

【讨论】:

    【解决方案2】:

    而不是在url中添加:

    &oauth_token=令牌

    使用标题:

    授权:不记名令牌

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-08-02
      • 2011-06-23
      • 1970-01-01
      • 1970-01-01
      • 2019-03-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多