2010-10-11 52 views
3

我已经花了最后几天将我的头撞墙,以支持在我的Rails 3应用程序中添加联系人到Google Contacts API的能力。 http://everburning.com/news/google-analytics-oauth-and-ruby-oh-my/Ruby OAuth Nightmare:使用联系人API

当我按照这个控制台,我得到更多的比我在我的Rails应用程序做的:尽管有许多错误的开始,我终于通过采用红宝石OAuth的宝石,这里在介绍之后取得了一些进展。我可以创建一个访问令牌,使用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 =真正的”头从何而来,以及我将如何摆脱它?

但它在Rails应用程序中更糟糕。我有一个创建请求令牌,并导致用户认证站点与谷歌一个控制器动作(“googlecontacts”):

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='[email protected]_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] - 存在,占在行动!我无法弄清楚这里什么是零。

所以我想我需要先弄清楚第二个问题,但回答第一个问题的奖励点也是如此。 :-)

感谢您的阅读。

亚伦。

+0

呸,我发现这个问题。我使用了以前的开发人员的代码,并且他在environment.rb中隐藏了对Marshal方法的重新定义。删除并清除了所有内容。浪费时间! – 2010-10-11 15:04:17

+0

只需注意:“readbody = true”不是标题。它实际上是响应对象的Ruby实现的一部分。这从来没有被任何一方通过电报发送过,并且与您遇到的问题完全无关。 – 2010-11-22 21:40:50

回答

0

尝试使用字符串非符号设置/获取会话数据,即session["request_token"]而不是session[:request_token]。我知道我以前有过这个问题。

+0

对不起,这没有帮助。会话值清楚地显示在操作中(调用“logger.debug”REQUEST:“+ @request_token。检查“产生正确的数据),并且与params值相同。我的猜测是对get_access_token的调用,因为我找不到任何线路错误!但我不知道如何解决代码中的错误gem ... – 2010-10-11 14:20:17

+0

那么'google_auth',session [:request_token]'也有正确的数据吗?如果是这样,请发布整个堆栈跟踪。 – 2010-10-11 14:28:44

0

Unknown authorization header通常表示您的签名与您发送的内容不符。我不建议使用oauth宝石。它充满了错误和奇怪的问题,并且不能正确地转义某些参数。

Signet gem是官方支持的在Ruby中访问Google API的gem。

这里是你如何与徽记实现这个:

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)