【发布时间】:2014-09-19 22:02:38
【问题描述】:
我正在尝试获取个人资料列表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": "admin@domain.com",
"totalResults": 38,
"startIndex": 1,
"itemsPerPage": 1000,
"items": [
...
]
}
这是我使用 https://github.com/google/google-api-ruby-client/ gem 的示例 ruby 代码。
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="filtered@developer.gserviceaccount.com", @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\":\"filtered@developer.gserviceaccount.com\",\"totalResults\":25,\"startIndex\":1,\"itemsPerPage\":1000,\"items\":...
从控制台只有 25 条记录,为什么会这样?我尝试使用 max-items 选项,但没有运气,有什么提示吗?
【问题讨论】:
-
您是否查看过代码结果中缺少哪些视图?我的第一个想法是您正在请求两个不同的用户帐户,我知道这在过去发生在我身上......
标签: ruby-on-rails ruby google-analytics google-analytics-api google-api-client