我正在尝试获取配置文件列表 https://developers.google.com/analytics/devguides/config/mgmt/v3/mgmtReference/management/profiles/list
以下是网络版本的示例:
Request
GET https://www.googleapis.com/analytics/v3/management/accounts/~all/webproperties/~all/profiles?key={YOUR_API_KEY}
Authorization: Bearer
X-JavaScript-User-Agent: Google APIs Explorer
Response
200 OK
- Hide headers -
Cache-Control: private, max-age=0, must-revalidate, no-transform
Content-Encoding: gzip
Content-Type: application/json; charset=UTF-8
Date: Tue, 09 Sep 2014 16:20:18 GMT
Etag: "oq4YecK1DDgQfhLS-HzmxjZUB9I/ooSCrThtdvH0a3h5ysvIA31TDu0"
Expires: Tue, 09 Sep 2014 16:20:18 GMT
Server: GSE
Transfer-Encoding: Identity
{
"kind": "analytics#profiles",
"username": "<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="86e7e2ebefe8c6e2e9ebe7efe8a8e5e9eb" rel="noreferrer noopener nofollow">[email protected]</a>",
"totalResults": 38,
"startIndex": 1,
"itemsPerPage": 1000,
"items": [
...
]
}
这是我使用 https://github.com/google/google-api-ruby-client/ 的示例 ruby 代码 gem 。
def self.ga_client
client = Google::APIClient.new(
application_name: configatron.google_analytics.application_name,
application_version: configatron.google_analytics.application_version
)
key_file = File.join(configatron.google_analytics.pk12_file_path)
key = Google::APIClient::PKCS12.load_key(key_file, 'notasecret')
service_account = Google::APIClient::JWTAsserter.new(
configatron.google_analytics.service_email,
configatron.google_analytics.scope,
key
)
client.authorization = service_account.authorize
client
end
client = self.ga_client
analytics = client.discovered_api('analytics', configatron.google_analytics.version)
result = client.execute(
api_method: analytics.management.profiles.list,
parameters: {
accountId: "~all",
webPropertyId: "~all"
}
)
Response
#<Google::APIClient::Result:0x00000108c71a10 @request=#<Google::APIClient::Request:0x00000108cc3f90 @parameters={"accountId"=>"~all", "webPropertyId"=>"~all"}, @headers={"User-Agent"=>"DLM/1.0 google-api-ruby-client/0.7.1 Mac OS X/10.9.3\n (gzip)", "Accept-Encoding"=>"gzip", "Content-Type"=>""}, @api_method=#<Google::APIClient::Method:0x8474c6b8 ID:analytics.management.profiles.list>, @authenticated=nil, @authorization=#<Signet::OAuth2::Client:0x000001013435a8 @token_credential_uri=#<Addressable::URI:0x809a19e4 URI:https://accounts.google.com/o/oauth2/token>, @expiry=60, @extension_parameters={}, @additional_parameters={}, @scope=["https://www.googleapis.com/auth/analytics.readonly", "https://www.googleapis.com/auth/prediction"], @issuer="<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="3056595c445542555470545546555c5f4055421e57435542465953555153535f455e441e535f5d" rel="noreferrer noopener nofollow">[email protected]</a>", @principal=nil, @audience="https://accounts.google.com/o/oauth2/token", @signing_key=#<OpenSSL::PKey::RSA:0x00000101341000>, @grant_type=nil, @refresh_token=nil, @code=nil, @issued_at=2014-09-09 20:19:07 +0400, @expires_in=3600, @access_token="ya29.ewBSHe0Wh5oGeKoe8aJtdpzVb-Nhr9SF0O39mdE1HgF3zTKs-8wBHL5M">, @body="">, @response=#<Faraday::Response:0x00000108c798c8 @on_complete_callbacks=[], @env=#<Faraday::Env @method=:get @body="{\"kind\":\"analytics#profiles\",\"username\":\"<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="8bede2e7ffeef9eeefcbefeefdeee7e4fbeef9a5ecf8eef9fde2e8eeeae8e8e4fee5ffa5e8e4e6" rel="noreferrer noopener nofollow">[email protected]</a>\",\"totalResults\":25,\"startIndex\":1,\"itemsPerPage\":1000,\"items\":...
从控制台来看,只有 25 条记录,为什么会这样?我尝试使用 max-items 选项,但没有运气,有什么提示吗?
最佳答案
Google API 为您提供选项参数
- max-results:代表itemsPerPage。默认值为1000
- start-index:代表页码。默认值为 1
因此,我认为更改 max-items(max-results) 的值不会有帮助,因为您只有 35 条记录。使用 Google 控制台,我们使用不同的帐户 ( [email protected] ) & 对于我们使用的 API ( [email protected] )
我尝试使用谷歌客户端使用上述选项,它显示了正确的结果。
我该怎么做?
- 我总共有 13 个 GA 帐户 ( [email protected] )。
- 授予 11 个帐户分析只读权限[email protected]
- 尝试使用 GA 客户端库,它向我显示了 totalResults=11 及其项目详细信息。
- 使用 Views (Profiles): list 通过 OAuth 2.0 权限检查了我的个人资料结果玩。它显示了totalResults=13条记录
- 通过检查服务名称交叉验证 Google 客户端 API 记录。它向我显示了我已授予开发人员电子邮件帐户 [email protected] 访问权限的相同服务。
我确信您已按照以下步骤操作。请再次交叉验证。
- 转到Google API Console并创建一个新项目
在“API 访问”选项卡中,单击“创建 OAuth2.0 客户端 ID”
- 选择“服务帐户”选项,然后按“创建客户端 ID”
- 下载私钥
- 复制服务帐户电子邮件地址,即
XXXX@@developer.gserviceaccount.com
- 访问您的GA Admin并将此电子邮件地址作为用户添加到您的媒体资源
- 这是必须的;否则你会得到神秘的错误。
使用 Google Enable Api 向我的项目授予 Google Analytics 权限
获取最新的 Google Ruby Client API通过 gem
source "https://rubygems.org" gem 'google-api-client', '>= 0.6'
您的 API 访问代码。我的如下
require 'google/api_client' require 'json' API_VERSION = 'v3' CACHED_API_FILE = "analytics-#{API_VERSION}.cache" # Update these to match your own apps credentials service_account_email = '<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="59010003193d3c2f3c3536293c2b773e2a3c2b2f303a3c383a3a362c372d773a3634" rel="noreferrer noopener nofollow">[email protected]</a>' key_file = 'client.p12' # File containing your private key key_secret = 'notasecret' # Password to unlock private key client = Google::APIClient.new( :application_name => 'Plus Test', #Application name :application_version => '1') # Load our credentials for the service account key = Google::APIClient::KeyUtils.load_from_pkcs12(key_file, key_secret) client.authorization = Signet::OAuth2::Client.new( :token_credential_uri => 'https://accounts.google.com/o/oauth2/token', :audience => 'https://accounts.google.com/o/oauth2/token', :scope => ['https://www.googleapis.com/auth/analytics','https://www.googleapis.com/auth/analytics.edit','https://www.googleapis.com/auth/analytics.readonly'], :issuer => service_account_email, :signing_key => key) # Request a token for our service account client.authorization.fetch_access_token! analytics = nil # Load cached discovered API, if it exists. This prevents retrieving the # discovery document on every run, saving a round-trip to the discovery service. if File.exists? CACHED_API_FILE File.open(CACHED_API_FILE) do |file| analytics = Marshal.load(file) end else analytics = client.discovered_api('analytics', API_VERSION) File.open(CACHED_API_FILE, 'w') do |file| Marshal.dump(analytics, file) end end result = client.execute(:api_method => analytics.management.profiles.list, :parameters => { 'alt' => "json", 'accountId' => "~all", 'webPropertyId' => "~all", 'fields' => 'items(id,name,permissions,websiteUrl),itemsPerPage,startIndex,totalResults,username', 'max-results' => 1, #used to get number of records. Default value is 1000 'start-index' => 1 #This is page number. Default value is 1 }) #Response result result = JSON.parse(result.data.to_json) print result
使用
bundle exec ruby test.rb
执行代码
- 以上代码打印关联帐户的 json。
结论:您可能错过了向开发者电子邮件的某些属性/帐户授予 GA 权限。请使用 [email protected] 交叉验证实际 GA 帐户 ( [email protected] ) 配置文件列表
关于ruby-on-rails - 谷歌分析API中totalItems和实际项目的差异,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25847859/