我正在尝试在我的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视图中。不知道出了什么问题。
发布于 2014-06-10 16:10:07
对于使用Google Analytics的服务帐户,必须在帐户级别添加服务帐户电子邮件。如果只在视图级别添加它,它将不起作用。
https://stackoverflow.com/questions/24103425
复制相似问题