我覺得您遇到的問題是,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)'的錯誤' –