首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >totalItems与中实际项目的差异

totalItems与中实际项目的差异
EN

Stack Overflow用户
提问于 2014-09-15 12:13:21
回答 1查看 366关注 0票数 2

我在试着获得个人资料列表https://developers.google.com/analytics/devguides/config/mgmt/v3/mgmtReference/management/profiles/list

以下是web版本的示例:

代码语言:javascript
复制
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的红宝石代码示例。

代码语言:javascript
复制
  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张唱片,为什么?我试着玩最大物品选项,但没有运气,有什么提示吗?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2014-09-19 07:30:46

Google为您提供选项参数。

  • 结果:它表示itemsPerPage.Default值为1000。
  • 开始索引:它表示页面编号.默认值为1

因此,我不认为更改最大值(最大值-结果)的值会有帮助,因为你只有35条记录。使用google,我们使用的是不同的帐户(xyz@gmail.com)&对于我们使用的API (XXX@developer.gserviceaccount.com)

我尝试使用上面的选项使用谷歌客户端,它给我显示了正确的结果。

我是怎么做的?

  1. 我总共有13个GA帐户(xyz@gmail.com)。
  2. 给予XXX@developer.gserviceaccount.com 11个帐户分析只读权限
  3. 尝试使用GA客户端库,它向我展示了totalResults=11和它的项目细节。
  4. 使用OAuth 2.0权限使用视图(概况):列表 play检查我的配置文件结果。它给我看了totalResults=13记录
  5. 通过检查服务名称交叉验证记录。它向我展示了我为开发人员电子邮件帐户XXX@developer.gserviceaccount.com.提供的相同服务。

我相信你一定遵循了以下步骤。请再过一次确认。

  1. 转到Google控制台并创建一个新项目
  2. 在API Access选项卡中,单击“创建OAuth2.0客户端ID”
代码语言:javascript
复制
- Select the Service account option and press Create client ID
- Download private key
- Copy the Service Account Email address i.e. `XXXX@@developer.gserviceaccount.com`
- Visit your [GA Admin](https://www.google.com/analytics/web/#management/Accounts/) and add this email as a user to your properties
- This is a must; you'll get cryptic errors otherwise.

  1. 让Google允许我的项目使用Google Enable Api
  2. 通过Gem获取最新的Google Ruby客户端API 源"https://rubygems.org“gem‘google client’,'>= 0.6‘
  3. 您的API访问代码。我的如下所示 要求'google/api_ client‘要求'json’API_VERSION = 'v3‘CACHED_API_FILE = "analytics-#{API_VERSION}.cache“#更新这些以匹配您自己的应用程序凭据service_account_email = 'XYZ@developer.gserviceaccount.com’key_file = 'client.p12‘#文件,其中包含您的私钥key_secret = 'notasecret’#密码以解锁私钥client= Google::APIClient.new( :application_name => 'Plus Test‘),#应用程序名称:application_version => '1') #加载服务帐户键的凭据=:application_version key_secret) client.authorization = Signet::OAuth2::Client.new( :token_credential_uri => 'https://accounts.google.com/o/oauth2/token',::application_version => 'https://accounts.google.com/o/oauth2/token',:scope =>‘https://accounts.google.com/o/oauth2/token')'https://www.googleapis.com/auth/analytics.readonly',:发行人=> service_account_email,:signing_key =>密钥)#为我们的服务帐户client.authorization.fetch_access_token请求一个令牌!如果存在,analytics = nil #加载缓存已发现的API。这样可以防止每次运行时检索#发现文档,从而节省到发现服务的往返。如果File.exists?CACHED_API_FILE File.open(CACHED_API_FILE) do \file file= Marshal.load(file) end result = client.discovered_api(' analytics ',API_VERSION) File.open(CACHED_API_FILE,'w') do file x\Marshal.dump(分析,文件)终结结果= client.execute(:api_method => analytics.management.profiles.list,参数=> { 'alt‘=> "json",'accountId’=>“~all,'webPropertyId‘=> "~all",’=>‘字段’项目(id,name,=>,websiteUrl),itemsPerPage,startIndex,totalResults,username,‘max-=>’=> 1,#用于获取记录的数量。默认值是1000 'start-index‘=> 1#这是页码.默认值为1}#响应结果结果= JSON.parse(result.data.to_json)打印结果
  4. 使用bundle exec ruby test.rb执行代码
  5. 上面的代码打印关联帐户的json。

结论:可能错过了对某些属性/帐户的许可,用于开发人员的电子邮件。请用XXX@developer.gserviceaccount.com交叉验证实际的GA帐户(xyz@gmail.com)配置文件列表

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/25847859

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档