我正在尝试在我的Google Analytics帐户和我的Rails应用之间建立server-to-server连接。为此,我使用了Legato,omniauth-google-oauth2和google-api-client gem。我的目的是进行一个rake任务,从特定网站中筛选出网页浏览数据。但是,我似乎无法从中获取任何用户数据。这是代码:
require 'google/api_client'
def service_account_user(scope="https://www.googleapis.com/auth/analytics.readonly")
client = Google::APIClient.new(
:application_name => "Listmaker",
:application_version => "2.0"
)
key = OpenSSL::PKey::RSA.new(Figaro.env.google_private_key, "notasecret")
service_account = Google::APIClient::JWTAsserter.new(Figaro.env.google_app_email_address, 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)
Legato::User.new(token)
end
class Pageviews
extend Legato::Model
metrics :pageviews
dimensions :page_path
filter :for_page_path, &lambda {|page_path| matches(:page_path, page_path)}
end
puts profile = service_account_user.profiles.first
在运行任务后,我似乎正在为配置文件变量获取一个空数组。我确实已将开发者电子邮件地址添加到我感兴趣的Google Analytics View中。不确定是什么问题。
答案 0 :(得分:1)
要使用与Google Analytics配合使用的服务帐户,必须在帐户级别添加服务帐户电子邮件。如果仅在视图级别添加它,它将无法工作。