Skip to main content
 首页 » 编程设计

google-analytics之使用 PHP 对 Google API 进行 OAuth2 身份验证

2025年05月04日17yyy_WW

我需要为一家营销公司编写一个网络应用程序,该公司需要连接到他们的客户网站以访问谷歌分析数据并将其存储在他们的数据库中。
我按照 Google 在以下位置提供的示例进行操作:
https://developers.google.com/analytics/solutions/articles/hello-analytics-api
我在 Google API 控制台创建了以下客户端 ID

Client ID:      915242733409.apps.googleusercontent.com 
Email address:  915242733409@developer.gserviceaccount.com 
Client secret:  xxx 
Redirect URIs:  http://thefridgedoors.net/google-analytics/google-api-php-client/examples/analytics/simple.php 
JavaScript origins: https://www.thefridgedoors.net 

我的应用程序的身份验证如下所示:
/* Start a session to persist credentials */ 
session_start(); 
 
/* Create and configure a new client object */ 
$client = new Google_Client(); 
$client->setApplicationName("Google+ PHP Starter Application"); 
// Visit https://code.google.com/apis/console to generate your 
// oauth2_client_id, oauth2_client_secret, and to register your oauth2_redirect_uri. 
$client->setClientId('915242733409.apps.googleusercontent.com'); 
$client->setClientSecret('…'); 
$client->setRedirectUri('http://thefridgedoors.net/google-analytics/example1.php'); 
$client->setDeveloperKey('…'); 
$client->setScopes(array('https://www.googleapis.com/auth/analytics.readonly')); 

但是我收到以下错误:
有一个一般错误:
Error calling GET https://www.googleapis.com/analytics/v3/management/accounts?key=AIzaSyALRCwV32b8fAtcvFuaDVwNUxrYrtg51tI: (403) Access Not Configured 

有什么可能出错的提示吗?

请您参考如下方法:

您需要在 Google APIs Console 中为您的项目启用 Analytics API .打开你的项目,选择服务 选项卡,然后单击 下的开关状态 Analytics API 列。

附言您可能希望避免发布您的客户 secret 。