Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Google Analytics API - Cant add services after having authenticated

Within the google console I have created Credentials for an API Key - Type Server and for Oauth with Type Other.

For the developer key I am using the API Key Credentials and for the client id/secret I am using the Oauth Credentials

I am using the php sdk for the google analytics api:

$client = new Google_Client();
$client->setApplicationName('Schedule GA');
$client->setAccessType('offline');
$client->setUseObjects(true);

$ganalytics_settings = wp_get_custom_field_for_current_user('ganalytics_settings', 'ga_settings');

$ganalytics_settings['google_api_key'] = 'c0f3d189e82938128ndoea1a426ee4e264e4b0b0';
$ganalytics_settings['google_client_id'] = '17381202384367-gejnedh2aijuq1660f0lvl5uvj6roloo4.apps.googleusercontent.com';
$ganalytics_settings['google_client_secret'] = '**Secret key**';

$client->setClientId("17381202384367-gejnel2aijuq1060f0lvl5uvj6roloo4.apps.googleusercontent.com");
$client->setClientSecret("QclsDKOSLcsrbpJD_KLbIUtQ");
$client->setDeveloperKey("c0f3d189e82938128ndoea1a426ee4e264e4b0b0");
$client->setRedirectUri('urn:ietf:wg:oauth:2.0:oob');
$analytics = new Google_AnalyticsService($client); //the error occurs here!!!

// Setting Access Token

$access_token = $ganalytics_settings['google_access_token'];
if ($access_token) {
    $client->setAccessToken($access_token);
}
else {
    if ($ganalytics_settings['google_auth_code']) {
        $client->authenticate($ganalytics_settings['google_auth_code']);
        $ganalytics_settings['google_access_token'] = $client->getAccessToken();

        // update_option('ganalytics_settings', $ganalytics_settings);
        ga_settings_exists_else_update();
    }
}

However, I get the following error message:

Cant add services after having authenticated

Any suggestion what I am doing wrong?

I appreciate your reply!

like image 769
Carol.Kar Avatar asked Aug 12 '16 05:08

Carol.Kar


People also ask

How do I add authorization to API?

With API key auth, you send a key-value pair to the API either in the request headers or query parameters. In the request Authorization tab, select API Key from the Type list. Enter your key name and value, and select either Header or Query Params from the Add to dropdown list.


1 Answers

You need to set a scope first, example:

$client->setScopes(['https://www.googleapis.com/auth/analytics.readonly']);

This tutorial might help you in the future.

like image 193
Nick Avatar answered Sep 20 '22 07:09

Nick