I downloaded the API files from https://github.com/google/google-api-php-client and tried under the code ...
$client->setApplicationName("Analytics"); $client->setDeveloperKey("key"); $service = new Google_Service_Analytics($client); $optParams = array( 'dimensions' => 'rt:medium'); try { $results = $service->data_realtime->get( 'ga:profileid', 'rt:activeUsers', $optParams);
The error message (401) "Login Required" will appear
When I tried the call from https://console.developers.google.com/project/apps~rational-photon-578/apiui/api/analytics/method/analytics.data.realtime.get
I have the necessary real-time users that belong to the profiler.
So how can I implement the login process (without going to google site)?
I tried the code below ...
$client_id = 'xxxxxxxxxxxxxxx'; $service_account_name = ' xxxxxxxxxxxxxxxx@developer.gserviceaccount.com '; $key_file_location = 'location of .p12 keyfile'; $client = new Google_Client(); $client->setApplicationName("Client_Library_Examples"); if (isset($_SESSION['service_token'])) { $client->setAccessToken($_SESSION['service_token']); } $key = file_get_contents($key_file_location); $cred = new Google_Auth_AssertionCredentials( $service_account_name, array('https://www.googleapis.com/auth/analytics'), $key ); $client->setAssertionCredentials($cred); if($client->getAuth()->isAccessTokenExpired()) { $client->getAuth()->refreshTokenWithAssertion($cred); } $_SESSION['service_token'] = $client->getAccessToken(); echo $_SESSION['service_token']; $service = new Google_Service_Analytics($client); $optParams = array( 'dimensions' => 'rt:medium'); try { $results = $service->data_realtime->get( 'ga:profileid', 'rt:activeUsers', $optParams);
Here I get access_token and details ... but the extraction of analytical data occurs due to the error "Resolving immunity"
I got the results when I did a test from the Google console with the specified profile ID.
source share