2012-06-02 94 views
4

我需要连接到Google Analytics。我使用omniauth_google_oauth2来验证与应用程序的用户,这给了我一个令牌。问题是我需要连接到此用户的Google Analytics帐户。要将GA连接到我的应用程序,我使用了garb gem,它只有两种身份验证方法;用户名/密码和OAuth令牌。当我使用omniauth_google_oauth2提供的令牌时,它不起作用。如何将garb与来自omniauth_google_oauth2的令牌连接起来?

如何仅使用通过omniauth_google_oauth2进行身份验证所获得的oauth_token来创建此新令牌?

回答

1

我觉得您遇到的问题是,garb只会验证使用OAuth 1(或用户名/密码组合)的用户,而omniauth_google_oauth2是(明显)的OAuth 2

我唯一的解决办法已经找到的使用谷歌的已过时的OAuth 1的情况如下...

的Gemfile:

gem 'omniauth-google', :git => 'git://github.com/davidkpham/omniauth-google.git' 
# This fork relaxes dependencies on omniauth itself 

初始化程序(用于谷歌Analytics(分析)访问):

provider :google, 'XXXXXXXXXXXX.apps.googleusercontent.com', 'YOUR_SECRET', scope: 'https://www.googleapis.com/auth/userinfo.email https://www.googleapis.com/auth/userinfo.profile https://www.google.com/analytics/feeds/' 

在回调,存放一些东西传递回:

auth = request.env["omniauth.auth"] 
session[:google_token] = auth.credentials.token 
session[:google_secret] = auth.credentials.secret 

然后构建一个对的accessToken garb

if session[:google_token] and session[:google_secret] 
    consumer = OAuth::Consumer.new('XXXXXXXXXXXX.apps.googleusercontent.com', 'YOUR_SECRET', { 
    :site => 'https://www.google.com', 
    :request_token_path => '/accounts/OAuthGetRequestToken', 
    :access_token_path => '/accounts/OAuthGetAccessToken', 
    :authorize_path => '/accounts/OAuthAuthorizeToken' 
    }) 
    garbsession = Garb::Session.new 
    garbsession.access_token = OAuth::AccessToken.new(consumer, session[:google_token], session[:google_secret]) 
    # Once we have an OAuth::AccessToken constructed, do fun stuff with it 
    ga_id = "UA-XXXXXXX-X" 
    profile = Garb::Management::Profile.all(garbsession).detect {|p| p.web_property_id == ga_id} 
    ga_monthly = GoogleAnalyticsDate.results(profile, :start_date => (Date.today - 30), :end_date => Date.today, :sort => :date) 
    puts ga_monthly 
end 
+0

感谢伟大的答案,它救了一吨的我的时间。如果任何人有配置麻烦,我有一个示例应用程序,部分基于这个答案:https://github.com/eicca/ga_test –

+0

这是否仍然工作?我使用Signet为服务帐户获取OAuth2访问令牌(因为我需要服务器到服务器),然后创建一个新的Garb :: Session对象并以与答案相同的方式为其分配访问令牌。我收到一个'Garb :: Management :: Profile.all(garbsession)'的错误' –

2

我知道我迟到了这个一但我解决了类似的问题。除非使用支持oauth2的garb叉子,否则不能使用garb的omniauth_google_oauth2。有一个here由Sija很好维护。但是,您需要使用oauth2客户端对象才能使用此叉创建会话。您可以使用omniauth_google_oauth2设置用户的个人资料,并确保为用户保存刷新令牌,然后当您想用garb获取分析数据时,使用oauth2刷新令牌,然后将该对象传递到服装会话中,以便将用户的数据。这里有一个例子,你有refresh_token之后,从omniauth存储的地方:

client = OAuth2::Client.new YOURGOOGLEAPIKEY, YOURGOOGLEAPISECRET, 
           { 
            :site => 'https://accounts.google.com', 
            :authorize_url => "/o/oauth2/auth", 
            :token_url => "/o/oauth2/token", 
           } 
response = OAuth2::AccessToken.from_hash(client, :refresh_token => omniauth_refresh_token).refresh! 
Garb::Session.access_token = response 
+1

我很乐意为你做一点点扩展。我认为与Sija的Garb叉的OAuth2是解决这个问题的正确方法。我似乎无法将此取消。 – adamb0mb

+0

你有什么特别的问题吗?它是从omniauth_google_oauth2获取refresh_token,刷新它,设置sija的fork还是使用刷新响应来启动garb会话? – topher

+0

我想通了......我想我只是有问题使它与'irb'一起工作。你的代码片段是现货。 – adamb0mb

相关问题