我使用Garb(0.9.8)从我们的服务器直接使用OAuth1从Google Analytics获取数据。由于Google现在只支持OAuth2,所以我切换到Signet gem并使用服务帐户直接从服务器获取access_token,因为我们只需要服务器到服务器的交互。
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游乐场仔细检查了它。服务帐户已被添加为具有所有权限的用户到我的谷歌分析帐户。如果可行的话,我想使用Garb而不是完全切换到google-api-client gem,以避免重写大量现有代码。然后,为了使Garb使用获得的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中的其他方法获得的访问令牌,然后访问谷歌分析API吗?
我犯的错误是Garb::Session.access_token
应该是OAuth2
客户机的实例,而不是访问令牌本身。我张贴的工作代码在我的回答,以便它可能会帮助其他人以后!在浏览Legato宝石寻找问题的解决方案时遇到了代码。Github问题链接从那里我找到了解决方案- https://github.com/tpitale/legato/issues/90
p12_key_path = File.join(Rails.root, "config", <p12_key_path>)
key = Google::APIClient::KeyUtils.load_from_pkcs12(p12_key_path, 'notasecret')
auth_client = 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.readonly',
issuer: <ga_service_account email>,
signing_key: key
)
access_token = auth_client.fetch_access_token!
raise "Google OAuth Access Token is blank" if access_token["access_token"].blank?
oauth_client = OAuth2::Client.new("", "", {
authorize_url: 'https://accounts.google.com/o/oauth2/auth',
token_url: 'https://accounts.google.com/o/oauth2/token'
})
token = OAuth2::AccessToken.new(oauth_client, access_token["access_token"], expires_in: 1.hour)
Garb::Session.access_token = token
profile = Garb::Management::Profile.all