调用Google Analytics Data API时提示缺少必填参数‘property’求助
解决GA4 Data API调用报错"Missing required parameter 'property'"的问题
问题出在API调用的请求体结构上:你把请求参数嵌套在了{'request': request}里,但Google Analytics Data API的runReport方法不需要这一层嵌套,直接传入请求体字典即可。
修正后的代码:
from google.oauth2 import service_account from googleapiclient.discovery import build # 替换为你的服务账号密钥路径 creds = service_account.Credentials.from_service_account_file(r"C:\Users\xxxx\xxx\xxx\xxx.json") # 创建API客户端 analytics_service = build('analyticsdata', 'v1beta', credentials=creds) # 定义GA4媒体资源的请求参数 request = { 'property': 'properties/xxxxx', 'dateRanges': [{'startDate': '7daysAgo', 'endDate': 'today'}], 'dimensions': [{'name': 'date'}], 'metrics': [{'name': 'activeUsers'}] } # 调用API try: # 去掉多余的'request'嵌套,直接传入request作为body response = analytics_service.properties().runReport(body=request).execute() print(response) except Exception as e: print(e)
补充说明
- 确保
properties/xxxxx中的xxxxx是你的GA4媒体资源纯数字ID - 确认服务账号已在GA4后台添加为媒体资源的查看者/编辑者权限,且GCP中已启用Google Analytics Data API
内容的提问来源于stack exchange,提问作者Fawzan Ahmed
相关产品推荐
相关产品推荐

