我们应该如何创建凭据以通过 Python 连接到 google 分析?

How we should create the credential to connect to google analytics via Python?

我使用 google Python example 连接到 google 分析并收集所有数据。使用此示例时,我能够连接并收集数据,但是当我将 profile_id 更改为 select 不同的视图时,出现以下错误:

是:

profile_id = get_first_profile_id(service)

现在:

profile_id = '68444917

googleapiclient.errors.HttpError: <HttpError 403 when requesting https://www.googleapis.com/analytics/v3/data/ga?ids=ga%3A168847831&start-date=2018-01-01&end-date=2019-02-01&metrics=ga%3Asessions%2Cga%3AtotalEvents%2Cga%3Ausers&dimensions=ga%3AeventAction%2Cga%3Adate%2Cga%3AcontentGroup2%2Cga%3AcontentGroup3&filters=ga%3AeventAction%3D%3DDealer+site&samplingLevel=HIGHER_PRECISION&include-empty-rows=true&start-index=1&max-results=10000&alt=json returned "User does not have sufficient permissions for this profile.">

管理员根据问题检查了我的权限1 and 2,看来我可以访问所有视图。

需要 google analytics 授予创建的电子邮件帐户的权限,如前所述 here:

The newly created service account will have an email address that looks similar to:

quickstart@PROJECT-ID.iam.gserviceaccount.com
Use this email address to add a user to the Google analytics view you want to access via the API. For this tutorial only Read & Analyze permissions are needed.