Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/264.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Google Analytics PHP API-错误(403)访问未配置_Php_Google Api_Google Analytics Api - Fatal编程技术网

Google Analytics PHP API-错误(403)访问未配置

Google Analytics PHP API-错误(403)访问未配置,php,google-api,google-analytics-api,Php,Google Api,Google Analytics Api,我已经在谷歌上搜索了好几个小时了。我一直在关注这个问题 应用程序运行正常,直到用户从Google“同意屏幕”授予权限,此时我收到以下错误: 有一个一般性错误:调用GETMy url时出错 (403)未配置访问权限。请使用谷歌开发者控制台 激活项目的API 我已经检查了开发者控制台,并且启用了Analytics API,在阅读了其他关于此的帖子后,我添加了Google+和驱动器访问 注意:我正在本地运行此应用程序,并将重定向重定向回127.0.0.1 我在下面添加了代码以供参考,尽管我认为问题与我

我已经在谷歌上搜索了好几个小时了。我一直在关注这个问题

应用程序运行正常,直到用户从Google“同意屏幕”授予权限,此时我收到以下错误:

有一个一般性错误:调用GETMy url时出错 (403)未配置访问权限。请使用谷歌开发者控制台 激活项目的API

我已经检查了开发者控制台,并且启用了Analytics API,在阅读了其他关于此的帖子后,我添加了Google+和驱动器访问

注意:我正在本地运行此应用程序,并将重定向重定向回127.0.0.1

我在下面添加了代码以供参考,尽管我认为问题与我的google console帐户和/或在本地运行此应用程序有关

谢谢你对这些人的帮助。如果需要更多信息,请告诉我

==================================================================================

// --------------------- Google libraries

require_once 'google-api-php-client/src/Google_Client.php';
require_once 'google-api-php-client/src/contrib/Google_AnalyticsService.php';

session_start();

// --------------------- Application credentials

$client = new Google_Client();
$client->setApplicationName('Interim Testing Tool');
$client->setClientId('xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx.apps.googleusercontent.com');
$client->setClientSecret('xxxxxxxxxxxxxxxxxxxxxxxx');
$client->setRedirectUri('http://127.0.0.1');
$client->setDeveloperKey('xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx');
$client->setScopes(array('https://www.googleapis.com/auth/analytics.readonly'));

// Returns objects from the Analytics Service instead of associative arrays.
$client->setUseObjects(true);

// --------------------- Client access checks

// Handle authorization flow from the server
if (isset($_GET['code'])) {
    $client->authenticate();
    $_SESSION['token'] = $client->getAccessToken();
    $redirect = 'http://' . $_SERVER['HTTP_HOST'] . $_SERVER['PHP_SELF'];
    header('Location: ' . filter_var($redirect, FILTER_SANITIZE_URL));
}

// Retrieve and use stored credentials if set
if (isset($_SESSION['token'])) {
    $client->setAccessToken($_SESSION['token']);
}

if (!$client->getAccessToken()) {
    // CLient not logged in create a connect button
    $authUrl = $client->createAuthUrl();
    print "<a class='login' href='$authUrl'>Connect Me!</a>";
} else {
    $analytics = new Google_AnalyticsService($client);
    runMainDemo($analytics);
}
//------------------------------------------------------------获取第一个配置文件id

function getFirstprofileId($analytics) {
    $accounts = $analytics->management_accounts->listManagementAccounts();

    if (count($accounts->getItems()) > 0) {
        $items = $accounts->getItems();
        $firstAccountId = $items[0]->getId();

        $webproperties = $analytics->management_webproperties->listManagementWebproperties($firstAccountId);

        if (count($webproperties->getItems()) > 0) {
            $items = $webproperties->getItems();
            $firstWebpropertyId = $items[0]->getId();
            $profiles = $analytics->management_profiles->listManagementProfiles($firstAccountId, $firstWebpropertyId);

            if (count($profiles->getItems()) > 0) {
                $items = $profiles->getItems();
                return $items[0]->getId();
            } else {
            throw new Exception('No views (profiles) found for this user.');
            }
        } else {
            throw new Exception('No webproperties found for this user.');
        }
    } else {
        throw new Exception('No accounts found for this user.');
    }
}

这有什么帮助吗?将尝试启用计费并报告回来(如果是这样的话,我会对谷歌非常恼火:P)。这在我之前就发生过。。。即使它是免费的,你所有的数据都属于谷歌。好的,我已经启用了biling并删除了我的推荐人,所以现在任何推荐人都是允许的。还有其他想法吗?我需要对我的IP做些什么吗?好奇其中关于本地IP与公共IP不匹配的部分是否重要--看看你是否可以强制它从你的外部IP发送?
function getFirstprofileId($analytics) {
    $accounts = $analytics->management_accounts->listManagementAccounts();

    if (count($accounts->getItems()) > 0) {
        $items = $accounts->getItems();
        $firstAccountId = $items[0]->getId();

        $webproperties = $analytics->management_webproperties->listManagementWebproperties($firstAccountId);

        if (count($webproperties->getItems()) > 0) {
            $items = $webproperties->getItems();
            $firstWebpropertyId = $items[0]->getId();
            $profiles = $analytics->management_profiles->listManagementProfiles($firstAccountId, $firstWebpropertyId);

            if (count($profiles->getItems()) > 0) {
                $items = $profiles->getItems();
                return $items[0]->getId();
            } else {
            throw new Exception('No views (profiles) found for this user.');
            }
        } else {
            throw new Exception('No webproperties found for this user.');
        }
    } else {
        throw new Exception('No accounts found for this user.');
    }
}