簡體   English   中英

Ruby OAuth夢魘:使用Contacts API

[英]Ruby OAuth Nightmare: Using Contacts API

過去幾天我一直在用力支持在Rails 3應用程序中添加聯系人到Google Contacts API的能力。 盡管有許多錯誤的開始,但我最終通過使用Ruby OAuth gem取得了一些進展,並遵循以下教程: http//everburning.com/news/google-analytics-oauth-and-ruby-oh-my/

當我在控制台中執行此操作時,我比在Rails應用程序中更進一步。 我可以創建一個訪問令牌,使用Contacts API的特定范圍對Google的服務進行身份驗證,並應用oauth_verifier令牌來獲取訪問令牌。 但是,當需要推送數據時,我收到此錯誤:

response = at.post("https://www.google.com/m8/feeds/contacts/default/full", gdata)
 => #<Net::HTTPUnauthorized 401 Unknown authorization header readbody=true> 

“readbody = true”標題來自何處,我將如何擺脫它?

但是在Rails應用程序中它更糟糕。 我有一個控制器操作(“googlecontacts”),它創建請求令牌並引導用戶使用Google訪問身份驗證站點:

def googlecontacts

@card = Card.find_by_short_link(params[:id])

@consumer = OAuth::Consumer.new( 
  'anonymous', 
  'anonymous', 
  { 
    :site => 'https://www.google.com', 
    :request_token_path => '/accounts/OAuthGetRequestToken', 
    :access_token_path => '/accounts/OAuthGetAccessToken', 
    :authorize_path => '/accounts/OAuthAuthorizeToken', 
    :signature_method => 'HMAC-SHA1',
    :oauth_version => '1.0'
  })

@request_token = @consumer.get_request_token(
  {:oauth_callback => 'http://monkey.dev/cards/google_auth?redir='+@card.short_link}, 
  {:scope => "https://www.google.com/m8/feeds/"}
)

session[:request_token] = @request_token

redirect_to @request_token.authorize_url

end

這似乎有效; 我得到一個工作請求令牌對象,並將用戶轉發到Google服務進行身份驗證。 回調網址(“google_auth”)應該使用oauth_verifier標記來創建訪問令牌。 這是控制器的開頭:

def google_auth

   @access_token = session[:request_token].get_access_token(:oauth_verifier=>params[:oauth_verifier])

而這里就是它瘋狂的地方。 最后一行的錯誤是:

You have a nil object when you didn't expect it!
You might have expected an instance of Array.
The error occurred while evaluating nil.[]

但是那里的值 - 會話[:request_token]和params [:oauth_verifier] - 在該行動中存在並被考慮在內! 我不知道這里有什么是零。

所以我想我首先需要弄清楚第二個問題,但也要回答第一個問題。 :-)

謝謝閱讀。

亞倫。

嘗試使用不是符號的字符串設置/獲取會話數據,即session["request_token"] ,而不是session[:request_token] 我知道我以前遇到過這個問題。

Unknown authorization header通常表示您的簽名與您發送的簽名不匹配。 我不推薦oauth gem。 它充滿了錯誤和奇怪的問題,並沒有正確地逃避某些參數。

Signet gem是官方支持的用於訪問Ruby中的Google API的gem。

以下是您使用Signet實現此操作的方法:

require 'signet/oauth_1/client'
require 'addressable/uri'
card = Card.find_by_short_link(params[:id])
callback = Addressable::URI.parse('http://monkey.dev/cards/google_auth')
callback.query_values = {'redir' => card.short_link}

client = Signet::OAuth1::Client.new(
  :temporary_credential_uri =>
    'https://www.google.com/accounts/OAuthGetRequestToken',
  :authorization_uri =>
    'https://www.google.com/accounts/OAuthAuthorizeToken',
  :token_credential_uri =>
    'https://www.google.com/accounts/OAuthGetAccessToken',
  :client_credential_key => 'anonymous',
  :client_credential_secret => 'anonymous',
  :callback => callback
)

session[:temporary_credential] = (
  client.fetch_temporary_credential!(:additional_parameters => {
    :scope => 'https://www.google.com/m8/feeds/'
  })
)
redirect_to(client.authorization_uri)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM