使用PHP进行Google API的OAuth2身份验证

时间:2013-07-19 02:31:51

标签: google-analytics google-api-php-client

我需要为营销公司编写一个网络应用程序,该公司需要连接到他们的客户网站以访问Google分析数据并将其存储在他们的数据库中。 我按照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

任何可能出错的提示?

1 个答案:

答案 0 :(得分:2)

您需要在Google APIs Console中为项目启用Analytics API。打开项目,选择服务标签,然后点击Analytics API的状态列下的切换。

P.S。您可能希望避免发布您的客户机密。