调用Google Play Developer Reporting API报403关联项目不匹配错误
问题背景
尝试调用Google Play Developer Reporting API v1beta1拉取Android Vitals统计数据,凭据使用系统管理员创建的服务账号。脚本发起数据查询时运行失败,抛出Google Cloud与Play Console关联相关错误;管理员反馈该服务账号已完成Google Play关联配置,检索公开技术资料未找到同类问题解决方案。
调用代码
from google.oauth2 import service_account from googleapiclient.discovery import build from google.oauth2.credentials import Credentials SCOPES = ["https://www.googleapis.com/auth/playdeveloperreporting"] credentials = service_account.Credentials.from_service_account_file('servise_account.json') scoped_credentials = credentials.with_scopes(SCOPES) reporting_user = build("playdeveloperreporting", "v1beta1", credentials=scoped_credentials, cache_discovery=False) body = {"dimensions": ["versionCode"], "metrics": ["anrRate", "anrRate7dUserWeighted", "distinctUsers"], "timelineSpec": {"aggregationPeriod": "DAILY", "endTime": { "day": 16, "month": 5, "year": 2022 }, "startTime": { "day": 10, "month": 5, "year": 2022 }}} d = reporting_user.vitals().crashrate().query(name="apps/com.example.game/crashRateMetricSet", body=body).execute() pprint(d)
报错信息
Traceback (most recent call last): File "/home/airflow/.local/lib/python3.8/site-packages/airflow/models/taskinstance.py", line 1112, in _run_raw_task self._prepare_and_execute_task_with_callbacks(context, task) File "/home/airflow/.local/lib/python3.8/site-packages/airflow/models/taskinstance.py", line 1285, in _prepare_and_execute_task_with_callbacks result = self._execute_task(context, task_copy) File "/home/airflow/.local/lib/python3.8/site-packages/airflow/models/taskinstance.py", line 1315, in _execute_task result = task_copy.execute(context=context) File "/home/airflow/.local/lib/python3.8/site-packages/airflow/operators/python.py", line 117, in execute return_value = self.execute_callable() File "/home/airflow/.local/lib/python3.8/site-packages/airflow/operators/python.py", line 128, in execute_callable return self.python_callable(*self.op_args, **self.op_kwargs) File "/opt/airflow/dags/anr_crash_share.py", line 89, in query d = reporting_user.vitals().crashrate().query(name="apps/com.example.game/crashRateMetricSet", body=body).execute() File "/home/airflow/.local/lib/python3.8/site-packages/googleapiclient/_helpers.py", line 134, in positional_wrapper return wrapped(*args, **kwargs) File "/home/airflow/.local/lib/python3.8/site-packages/googleapiclient/http.py", line 915, in execute raise HttpError(resp, content, uri=self.uri) googleapiclient.errors.HttpError: <HttpError 403 when requesting https://playdeveloperreporting.googleapis.com/v1beta1/apps/com.example.game/crashRateMetricSet?alt=json returned "The linked Google Cloud project does not match the project used to call this API. Requests to the Google Play Developer Reporting API must use a service account or authenticate through an OAuth client belonging to the linked Cloud project.". Details: "The linked Google Cloud project does not match the project used to call this API. Requests to the Google Play Developer Reporting API must use a service account or authenticate through an OAuth client belonging to the linked Cloud project.">
排查解决步骤
报错核心原因是调用API使用的服务账号所属GCP项目,和Play Console后台绑定的GCP项目不一致,和服务账号本身有没有开Play权限没有直接关系,按以下顺序核对即可:
- 让Play Console管理员进入后台「API访问」配置页,确认当前Play开发者账号实际绑定的GCP项目ID/编号,不要仅凭“已经完成关联”的口头反馈判断,必须拿到准确的项目ID。
- 打开本地使用的服务账号密钥文件
servise_account.json,查看其中project_id字段的值,确认该值和上一步查到的Play Console绑定项目ID完全一致。绝大多数场景下的该报错,都是因为服务账号属于其他未绑定的GCP项目——哪怕管理员把这个服务账号加到了Play Console的授权列表,只要所属项目不匹配,就会返回403。 - 如果两者ID不匹配,二选一处理即可:
- 在Play Console的API访问页修改绑定关系,链接到当前服务账号所属的GCP项目
- 在已经绑定的GCP项目中新建服务账号,下载对应密钥文件替换本地的密钥,同时在Play Console给新服务账号授予对应应用的Android Vitals数据读取权限
- 权限问题修复后,还要调整代码里的接口调用逻辑:当前代码调用的是
crashrate()查询崩溃率数据集,但body里传入的指标包含anrRate等ANR相关指标,会触发参数错误。查ANR数据需要调用anrrate()对应的接口,查询崩溃率数据时再使用crashrate()接口,两者的指标集不能混用。
内容的提问来源于stack exchange,提问作者Zapot
相关产品推荐
相关产品推荐

