我使用谷歌服务帐户对我的冲刺仪表板进行API调用。我使用Legato gem获取Analytics数据,并使用gem的wiki的服务帐户说明进行身份验证。
我已经将我的Google用户名和私钥放入ENV (在基本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上的时区设置为美国/芝加哥(与我的本地机器相同),但没有骰子。
有什么想法吗?谢谢!
发布于 2014-07-11 21:12:17
哈。我使用dotenv和heroku-config将我的设置推到Heroku。原来,把我的GOOGLE_USER和GOOGLE_PRIVATE_KEY_BASE64放在我的.env文件中的引号是heroku-config的字面意思,它将带有引号的设置推送到我的Heroku配置中。因此,用户名/密钥无效-因此是invalid_grant。
想想我已经解决这个问题好几天了.
https://stackoverflow.com/questions/24687194
复制相似问题