Google Analytics 服务帐户授权
GoogleAnalytics ServiceAccount auth
在尝试使用 Legato gem with service accounts 时,我们收到来自 Google Authorization failed. Server message: { "error" : "invalid_grant” }
的错误。
def token
OAuth2::AccessToken.new(oauth_client, client_authorization.access_token,
expires_in: 1.hour
)
end
def oauth_client
OAuth2::Client.new("", "", {
authorize_url: "https://accounts.google.com/o/oauth2/auth",
token_url: "https://accounts.google.com/o/oauth2/token",
})
end
def client_authorization
@_client_authorization ||= client.authorization = service_account.authorize
end
def service_account
Google::APIClient::JWTAsserter.new({{ secret email address }},
"https://www.googleapis.com/auth/analytics.readonly",
key)
end
def key
Google::APIClient::KeyUtils.load_from_pem({{ secret keyfile path }}, {{ not so secret keyfile passphrase }})
end
def client
Google::APIClient.new(
application_name: {{ app name }},
application_version: 1,
)
end
我们知道一些事情:
- keyfile/pass 正常工作。如果不是,我们会看到“无效的密钥文件或密码”。
- 代码在开发中有效(一致且符合预期)
假设问题:
- 我们可以只为服务帐户生成一次授权(每小时)吗?
- 我们是否需要以某种方式在服务器之间共享授权?
- 我们是否需要手动获取令牌并改为使用它?
- 是否有不适用于本地主机的 IP 限制?
有 2 个常见问题会导致 invalid_grant 错误:
- 您的服务器时钟与 NTP 不同步
- 您已超出刷新令牌限制
可以找到更多详细信息 here。
在尝试使用 Legato gem with service accounts 时,我们收到来自 Google Authorization failed. Server message: { "error" : "invalid_grant” }
的错误。
def token
OAuth2::AccessToken.new(oauth_client, client_authorization.access_token,
expires_in: 1.hour
)
end
def oauth_client
OAuth2::Client.new("", "", {
authorize_url: "https://accounts.google.com/o/oauth2/auth",
token_url: "https://accounts.google.com/o/oauth2/token",
})
end
def client_authorization
@_client_authorization ||= client.authorization = service_account.authorize
end
def service_account
Google::APIClient::JWTAsserter.new({{ secret email address }},
"https://www.googleapis.com/auth/analytics.readonly",
key)
end
def key
Google::APIClient::KeyUtils.load_from_pem({{ secret keyfile path }}, {{ not so secret keyfile passphrase }})
end
def client
Google::APIClient.new(
application_name: {{ app name }},
application_version: 1,
)
end
我们知道一些事情:
- keyfile/pass 正常工作。如果不是,我们会看到“无效的密钥文件或密码”。
- 代码在开发中有效(一致且符合预期)
假设问题:
- 我们可以只为服务帐户生成一次授权(每小时)吗?
- 我们是否需要以某种方式在服务器之间共享授权?
- 我们是否需要手动获取令牌并改为使用它?
- 是否有不适用于本地主机的 IP 限制?
有 2 个常见问题会导致 invalid_grant 错误:
- 您的服务器时钟与 NTP 不同步
- 您已超出刷新令牌限制
可以找到更多详细信息 here。