我使用Garb(0.9.8)直接使用OAuth1從我們的服務器從Google Analytics獲取數據。由於Google現在只支持OAuth2,所以我切換到Signet gem,並使用服務帳戶直接從服務器獲取access_token,因爲我們只需要服務器到服務器的交互。使用Garb獲取Google Analytics配置文件
key = Google::APIClient::KeyUtils.load_from_pkcs12(p12_key_path, 'notasecret')
client = Google::APIClient.new(application_name: 'Service account demo', application_version: '0.0.1')
client.authorization = Signet::OAuth2::Client.new(
:token_credential_uri => 'https://accounts.google.com/o/oauth2/token',
:audience => 'https://accounts.google.com/o/oauth2/token',
:scope => ['https://www.googleapis.com/auth/analytics', 'https://www.googleapis.com/auth/analytics.readonly'],
:issuer => <service account email>,
:signing_key => key
)
access_token = client.authorization.fetch_access_token!["access_token"]
該令牌是有效的,我從Google API操作系統中檢查過它。 該服務帳戶已添加爲具有對我的Google Analytics(分析)帳戶的所有權限的用戶。 如果可行,我想用Garb代替完全切換到google-api-client gem,以避免重寫大量現有代碼。 然後使裝束使用所獲得的和的access_token獲取所有配置文件我做了以下
garbsession = Garb::Session.new
garbsession.access_token = access_token
Garb::Management::Profile.all(garbsession)
但我得到一個錯誤:
NoMethodError: undefined method `get' for #<String:0xd877aa0>
from /home/alok/.rvm/gems/ruby-1.9.3-p385/gems/garb-0.9.8/lib/garb/request/data.rb:94:in `oauth_user_request'
from /home/alok/.rvm/gems/ruby-1.9.3-p385/gems/garb-0.9.8/lib/garb/request/data.rb:41:in `send_request'
from /home/alok/.rvm/gems/ruby-1.9.3-p385/gems/garb-0.9.8/lib/garb/management/feed.rb:21:in `response'
from /home/alok/.rvm/gems/ruby-1.9.3-p385/gems/garb-0.9.8/lib/garb/management/feed.rb:13:in `parsed_response'
from /home/alok/.rvm/gems/ruby-1.9.3-p385/gems/garb-0.9.8/lib/garb/management/feed.rb:17:in `entries'
from /home/alok/.rvm/gems/ruby-1.9.3-p385/gems/garb-0.9.8/lib/garb/management/profile.rb:14:in `all'
from (irb):47
from /home/alok/.rvm/gems/ruby-1.9.3-p385/gems/railties-3.2.19/lib/rails/commands/console.rb:47:in `start'
from /home/alok/.rvm/gems/ruby-1.9.3-p385/gems/railties-3.2.19/lib/rails/commands/console.rb:8:in `start'
from /home/alok/.rvm/gems/ruby-1.9.3-p385/gems/railties-3.2.19/lib/rails/commands.rb:41:in `<top (required)>'
from script/rails:6:in `require'
from script/rails:6:in `<main>'
我做錯什麼了嗎?我可以在Garb中使用通過其他方法獲取的Access令牌,然後訪問Google Analytics API嗎?
你可以改變你的代碼查找配置文件作爲Github的問題鏈接:裝束::管理:: Profile.all,除去外衣會議 –
我將指導您使用https:/ /github.com/tpitale/legato寶石分析的東西...它的一個真棒寶石。 –
@AdnanDevops - Garb :: Management :: Profile.all也不起作用。我會嘗試Legato,但從我看到它提供了一些我們不需要的功能。 –