Google Analytics PHP API - Error (403) Access Not Configured

ぐ巨炮叔叔 提交于 2019-12-13 03:12:20

问题


I have been Googling this for going on hours now. I have followed the Hello Analytics tutorial.

The application runs fine until the user has granted permission from the Google 'consent screen' at which point i receive the following error:

There wan a general error : Error calling GET My url (403) Access Not Configured. Please use Google Developers Console to activate the API for your project.

I have checked in the developer console and Analytics API is enabled and, after reading other posts on this, have added Google+ and Drive access.

NOTE: I am running this application locally and redirecting the redirecting back to 127.0.0.1.

I have added my code below for reference although I think the issue is to do with my google console account and / or running this application locally.

Appreciate any help with this guys. Please let me know if any further information is required.

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

// --------------------- 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);
}

//-------------------------------------------------------------- Run main demo

function runMainDemo($analytics) {
    try {
        // Get the user's first view (profile) ID.
        $profileId = getFirstProfileId($analytics);
        if (isset($profileId)) {
            // Query the Core Reporting API.
            $results = getResults($analytics, $profileId);
            // Output the results.
            printResults($results);
        }
    } catch (apiServiceException $e) {
        // Error from the API.
        print 'There was an API error : ' . $e->getCode() . ' : ' . $e->getMessage();
    } catch (Exception $e) {
        print 'There wan a general error : ' . $e->getMessage();
    }
}

// ------------------------------------------------------------------ Get first profile 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.');
    }
}

来源:https://stackoverflow.com/questions/21056303/google-analytics-php-api-error-403-access-not-configured

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!