2012-03-20 45 views
5

Tôi đã đọc kỹ tài liệu API Danh sách liên hệ của Google nhưng tôi không thể nhận được yêu cầu PUT (tức là bản cập nhật). Tôi đang sử dụng Ruby on Rails 3.2 với OAuth gem (v0.4.5). Tôi lấy thẻ với Omniauth và phạm vi được định nghĩa là "https://www.google.com/m8/feeds"API Danh sách liên hệ của Google: Không được phép 401 Tiêu đề ủy quyền không xác định

Hãy để tôi chứng minh:

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> 

Như bạn thấy, GET yêu cầu làm việc độc đáo để truy cập mã thông báo OAuth của tôi nên được alright. Các yêu cầu DELETE cũng hoạt động:

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> 

Cho đến giờ, rất tốt. Ở đây tôi đã cung cấp Etag của mục liên hệ trong tiêu đề yêu cầu theo hướng dẫn trong tài liệu của Google [1]. Vì vậy, nó không nên gây ra bất kỳ rắc rối nào.

Theo tài liệu OAuth đá quý [2] cú pháp của một yêu cầu PUT nên được như sau:

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

Và một ví dụ từ các tài liệu:

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

Theo như tôi hiểu , Tôi nên gửi một chuỗi XML đơn giản làm phần thân của yêu cầu PUT. Vì vậy, chúng ta hãy lấy một số dữ liệu ví dụ đầu tiên:

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>" 

... và cố gắng cập nhật lại cho 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> 

Vì vậy, đây không hoạt động. Hãy thử phân tích XML một chút để nó trông giống như trong tài liệu [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>" 

Nhưng không may mắn, chính xác lỗi giống như trước đây.

Đây là nơi tôi hoàn toàn bị kẹt ngay bây giờ. Tôi sẽ đánh giá rất cao bất kỳ gợi ý nào để đi đúng hướng.

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

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

+0

Bạn có thể thử định dạng yêu cầu trong một tệp và sau đó thực hiện yêu cầu bằng 'curl' để xem bạn có thể loại trừ một số phép thuật có thể xảy ra hay không. Một điều nảy sinh như một khả năng là một số thứ có thể đã được mã hóa (ví dụ: '" ') không nên như vậy. –

Trả lời

0

Hãy thử đặt tiêu đề Content-Type của bạn để 'application/nguyên tử + xml' trong yêu cầu PUT của bạn. Tôi đã có một vấn đề tương tự và làm điều đó đã giúp.

Các vấn đề liên quan