GA4 Admin API调用报错SERVICE_DISABLED/PERMISSION_DENIED求助
调用GA4 Admin API时遇到SERVICE_DISABLED/PERMISSION_DENIED异常排查及API版本咨询
问题场景
调用GA4 Admin的listAccountSummaries功能时,抛出包含SERVICE_DISABLED、PERMISSION_DENIED的异常,具体执行流程如下:
- 重定向至OAuth2授权地址:
https://accounts.google.com/o/oauth2/v2/auth?response_type=code&access_type=offline&client_id=<CLIENT ID>&redirect_uri=https%3A%2F%2Fedit.ihousestg.com%2Foauth2callback&state=host%3Dhttps%3A%2F%2Fwebhosts.ihousestg.com%2F-%2FControlGoogleAnalytics%2Foauth2callback&scope=https%3A%2F%2Fwww.googleapis.com%2Fauth%2Fanalytics.edit%20https%3A%2F%2Fwww.googleapis.com%2Fauth%2Fanalytics.readonly%20https%3A%2F%2Fwww.googleapis.com%2Fauth%2Fuserinfo.email%20https%3A%2F%2Fwww.googleapis.com%2Fauth%2Fanalytics.manage.users&approval_prompt=force&include_granted_scopes=true&login_hint=andy.ihouseweb%40gmail.com
- 获取回调code后,初始化并配置
Google\Client:
$this->google_client = new Google\Client(); $this->google_client->setApplicationName('GA4 Admin beta Oauth2'); $this->google_client->setAuthConfig($OauthKeyfile); $this->google_client->addScope(GoogleAnalyticsAdmin::ANALYTICS_EDIT); $this->google_client->addScope(GoogleAnalyticsAdmin::ANALYTICS_READONLY); $this->google_client->addScope(Oauth2::USERINFO_EMAIL); $this->google_client->addScope(Analytics::ANALYTICS_MANAGE_USERS); $this->google_client->setAccessType('offline'); $this->google_client->setApprovalPrompt("force"); $this->google_client->setIncludeGrantedScopes(true); $this->google_client->setRedirectUri('https://<MY SERVER>/oauth2callback');
- 通过code获取访问令牌:
$this->google_client->fetchAccessTokenWithAuthCode($code);
- 创建
AnalyticsAdminServiceClient并调用接口:
try { // make the API call. Log::Debug("Calling service_client->listAccountSummaries"); $results = $analytics_service_client->listAccountSummaries(); return $results; } catch(Exception $e) { Log::error("There was an Exception: " . $e->Message() ); throw $e; } finally { $analytics_service_client->close(); }
抛出的异常信息
Google\ApiCore\ApiException: { "reason": "SERVICE_DISABLED", "domain": "googleapis.com", "errorInfoMetadata": { "service": "analyticsadmin.googleapis.com", "consumer": "projects/WEIRD_PROJECT_ID" }, "message": "Google Analytics Admin API has not been used in project WEIRD_PROJECT_ID before or it is disabled. Enable it by visiting Google开发者控制台对应项目的Analytics Admin API概览页面 then retry. If you enabled this API recently, wait a few minutes for the action to propagate to our systems and retry.", "code": 7, "status": "PERMISSION_DENIED", "details": [ { "@type": "type.googleapis.com/google.rpc.Help", "links": [ { "description": "Google developers console API activation", "url": "Google开发者控制台API激活页面" } ] }, { "@type": "type.googleapis.com/google.rpc.ErrorInfo", "reason": "SERVICE_DISABLED", "domain": "googleapis.com", "metadata": { "service": "analyticsadmin.googleapis.com", "consumer": "projects/WEIRD_PROJECT_ID" } } ] }
备注信息
- 开发环境流程正常,测试环境出现该问题;测试环境控制台已启用Google Analytics Admin API、Google Analytics API、Google Analytics Data API,且已在项目下创建新Google账号用于授权。
- 当前使用API的alpha版本,需咨询beta版本的功能覆盖范围及正式版发布时间(需在数月内启用GA4)。
- OAuth密钥文件内容:
{"web": {"client_id":"MYCLIENTID.apps.googleusercontent.com", "project_id":"stage-1082", "auth_uri":"https://accounts.google.com/o/oauth2/auth", "token_uri":"https://oauth2.googleapis.com/token", "auth_provider_x509_cert_url":"https://www.googleapis.com/oauth2/v1/certs", "client_secret":MY_CLIENT_SECRET, "redirect_uris":["https://edit.ihousestg.com/oauth2callback"], "javascript_origins":["https://edit.ihousestg.com", "https://www.ihousestg.com", "https://ihousestg.com"] } }
请求协助
- 排查测试环境出现
SERVICE_DISABLED/PERMISSION_DENIED异常的原因。 - 解答GA4 Admin API beta版本的功能覆盖情况及正式版发布时间。
内容的提问来源于stack exchange,提问作者Andy Wallace
相关产品推荐
相关产品推荐

