简体   繁体   English

Google Analytics API PHP“ OAuth客户端已禁用”

[英]Google Analytics API PHP “The OAuth client was disabled”

I'm having trouble integrating google analytics api with my php web site. 我在将google analytics api与我的php网站集成时遇到问题。 I'm trying to show the data on my website without having users log into google, so I'm using the service account method. 我试图在不让用户登录google的情况下在我的网站上显示数据,所以我使用的是服务帐户方法。 I also tried to use the hello world analytics code, and that gives me the same error. 我还尝试使用hello world分析代码,这给了我同样的错误。

Here's the complete error message: Error refreshing the OAuth2 token, message: '{ "error" : "disabled_client", "error_description" : "The OAuth client was disabled." 这是完整的错误消息:刷新OAuth2令牌时出错,消息:'{“ error”:“ disabled_client”,“ error_description”:“ OAuth客户端已禁用。” }' }'

<?php 
require_once('google-api/Google_Client.php');
require_once('google-api/contrib/Google_AnalyticsService.php');

session_start();
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
</head>
<body>
<?php
$KEY_FILE = 'path_to_key_file/123-privatekey.p12'; //KEY FILE     

$client = new Google_Client();
$client->setApplicationName('AppName'); //APP NAME

$account = 'abcd@developer.gserviceaccount.com'; //FOUND IN CLIENT SERVICE EMAIL ADDRESS



$client->setAssertionCredentials(new Google_AssertionCredentials(
  $account,
  array('https://www.googleapis.com/auth/analytics.readonly'),
  file_get_contents($KEY_FILE))
);

$client->setClientId('123.apps.googleusercontent.com'); //CLIENT ID
$client->setAccessType('offline_access');  // this may be unnecessary?


if (!$client->getAccessToken() && false) {
  $authUrl = $client->createAuthUrl();
  print "<a class='login' href='$authUrl'>Connect Me!</a>";

} else {
  // Create analytics service object. See next step below.
    if ($client) 
    {
       // Create analytics service object. See next step below.
        //$analytics = new Google_AnalyticsService($client);
        //runDemo($analytics);
        $ids = "ga:" . "46052980";
        $startDate="2013-12-12";
        $endDate="2013-12-20";
        $metrics="ga:visits";
        try {
            $analytics = new Google_AnalyticsService($client);
            $results = $analytics->data_ga->get($ids,
                            $startDate,
                                $endDate,'ga:visits');
            /*echo '<b>Number of visits this week:</b> ';
            echo $results['totalsForAllResults']['ga:visits'];
            */
        } catch(Exception $e) {
            echo 'There was an error : - ' . $e->getMessage();
        }


    } else {
        echo "error";
        echo $key;
    }
}

if ($client->getAccessToken()) {
  $_SESSION['token'] = $client->getAccessToken();
  echo $_SESSION['token'];
}

?>
</body>
</html>

You have to use OAuth2 now. 您必须立即使用OAuth2。 Have a look at the sample code for PHP . 看一下PHP示例代码 Note that you have to register your app in the Google APIs Console first to get a client id, client secret, and API key. 请注意,您必须先在Google API控制台中注册您的应用,才能获取客户端ID,客户端密钥和API密钥。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM