You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Google Analytics Admin API设置accountSummaries.list分页大小报错求助

问题描述

根据Google Analytics Admin API的accountSummaries.list方法说明,该方法包含可选参数pageSize:

pageSize integer
返回AccountSummary资源的最大数量。即使存在更多分页,服务返回的数量也可能少于该值。未指定时最多返回50条资源,最大值为200(更高值会被强制转换为最大值)

以下示例代码中,不设置分页大小时可正常运行并获取所有数据,但设置分页大小时会提示参数不存在:

from google.analytics.admin import AnalyticsAdminServiceClient

# pip install google-analytics-admin

CREDENTIALS_FILE_PATH = 'C:\credentials.json'

from google_auth_oauthlib import flow

def list_accounts(credentials=None):
    """
    Lists the available Google Analytics accounts.

    Args:

    """


    # Using a default constructor instructs the client to use the credentials
    # specified in GOOGLE_APPLICATION_CREDENTIALS environment variable.
    client = AnalyticsAdminServiceClient(credentials=credentials)

    # Make the request
    results = client.list_account_summaries(page_size = 1)  # ERROR HERE

    # Displays the configuration information for all Google Analytics accounts
    # available to the authenticated user.
    print("Result:")
    for account in results:
        print(account)



def get_credentials():
    """Creates an OAuth2 credentials instance."""

    appflow = flow.InstalledAppFlow.from_client_secrets_file(
        CREDENTIALS_FILE_PATH,
        scopes=["https://www.googleapis.com/auth/analytics.readonly"],
    )
    # TODO(developer): Update the line below to set the `launch_browser` variable.
    # The `launch_browser` boolean variable indicates if a local server is used
    # as the callback URL in the auth flow. A value of `True` is recommended,
    # but a local server does not work if accessing the application remotely,
    # such as over SSH or from a remote Jupyter notebook.
    launch_browser = True
    if launch_browser:
        appflow.run_local_server()
    else:
        # note yes this is deprecated.  It will work if you change the redirect uri in the url given you from urn:ietf:wg:oauth:2.0:oob
        # to http://127.0.0.1  The client library team needs to fix this.
        appflow.run_console()
    return appflow.credentials



def main():
    credentials = get_credentials()
    list_accounts(credentials)

if __name__ == "__main__":
    main()

报错信息:

TypeError: list_account_summaries() got an unexpected keyword argument 'page_size'

尝试过pagesize、page_size、pageSize等多种参数格式均无效,查看相关项目文档后发现应该使用page_size,但依然报错。

Alpha/Beta版本与正式版本的差异

Alpha和Beta版本中可通过以下方式设置分页:

request = client.ListAccountSummariesRequest()
request.page_size = 1
results = client.list_account_summaries(request)

但在正式版本中使用该写法会报错:

AttributeError: 'AnalyticsAdminServiceClient' object has no attribute 'ListAccountSummariesRequest'

求助

如何在Google Analytics Admin API的正式版本中,正确设置accountSummaries.list方法的分页大小?


内容的提问来源于stack exchange,提问作者Linda Lawton - DaImTo

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.21 08:04:56