【发布时间】:2014-07-12 05:12:17
【问题描述】:
我正在使用 Google 服务帐号为我的 Dashing 仪表板向 Google Analytics(分析)发出 API 调用。我正在使用 Legato gem 来获取 Analytics 数据,并使用 gem 的 wiki 的 instructions for service accounts 进行身份验证。
我已将我的 Google 用户名和私钥放入 ENV(在对其进行 base 64 编码之后),并使用 dotenv 在本地和 Heroku 之间同步这些设置(heroku config 确认一切设置正确)。所以,我的验证码是这样的:
class GoogleAnalyticsAccount
attr_accessor :user, :profile
# Thanks to the "Service Accounts" section at
# https://github.com/tpitale/legato/wiki/OAuth2-and-Google
def initialize scope="https://www.googleapis.com/auth/analytics.readonly"
client = Google::APIClient.new application_name: '[App name]',
application_version: '1.0'
key = Google::APIClient::PKCS12.load_key(Base64.decode64(ENV['GOOGLE_PRIVATE_KEY_BASE64']), "notasecret")
service_account = Google::APIClient::JWTAsserter.new(ENV['GOOGLE_USER'], scope, key)
client.authorization = service_account.authorize
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, client.authorization.access_token)
@user = Legato::User.new(token)
end
def profile
@user.profiles.first
end
end
在本地,此代码运行良好。在 Heroku 上,我收到了来自 Google 的以下回复:
{
"error": "invalid_grant"
}
没有比这更详细的了。基于广泛的谷歌搜索,我发现这两个最可能的原因是 A)我已经达到了我的请求限制(但不可能是这样,因为相同的凭据在本地工作)和 B)服务器时钟未与 NTP 同步。我已将 Heroku 上的时区设置为 America/Chicago(与我的本地机器相同),但没有骰子。
有什么想法吗?谢谢!
【问题讨论】:
标签: ruby heroku oauth google-analytics google-api-client