我觉得您遇到的问题是,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
感谢伟大的答案,它救了一吨的我的时间。如果任何人有配置麻烦,我有一个示例应用程序,部分基于这个答案:https://github.com/eicca/ga_test –
这是否仍然工作?我使用Signet为服务帐户获取OAuth2访问令牌(因为我需要服务器到服务器),然后创建一个新的Garb :: Session对象并以与答案相同的方式为其分配访问令牌。我收到一个'Garb :: Management :: Profile.all(garbsession)'的错误' –