The solution to this problem is to create two seperate offline keys. I have done this without a database connect (but could easily had a database connect added in if you replace a part of this code). Please note that I am just learning PHP so please bare with code issues/indention issues.
In order for this to work Google must create two access tokens in two separate files.
Here is the example of the YouTube Analytics Generator (get_yt_access_token.php):
<?php
require_once 'google-api-php-client/src/Google_Client.php';
require_once 'google-api-php-client/src/contrib/Google_YouTubeAnalyticsService.php';
session_start();
$client = new Google_Client();
$client->setApplicationName('YT Analytics App');
$client->setClientId('');
$client->setClientSecret('');
$client->setRedirectUri('URL/get_yt_access_token.php');
$client->setScopes(array(
'https://www.googleapis.com/auth/yt-analytics.readonly',
'https://www.googleapis.com/auth/youtube.readonly'
));
$client->setAccessType('offline');
if (isset($_GET['code'])) {
$client->authenticate();
$_SESSION['token'] = $client->getAccessToken();
$myFile = "refreshyttoken.conf";
$fh = fopen($myFile, 'w') or die("can't open file");
fwrite($fh, $client->getAccessToken());
fclose($fh);
} else {
if (!$client->getAccessToken()) {
$auth = $client->createAuthUrl();
header("Location: $auth");
}
}
Notice the refreshmytoken.conf is the name of the file being generated.
Here is the example of the Google Analytics Generator (get_ga_access_token.php):
<?php
require_once 'google-api-php-client/src/Google_Client.php';
require_once 'google-api-php-client/src/contrib/Google_AnalyticsService.php';
session_start();
$client = new Google_Client();
$client->setApplicationName('GA Analytics App');
$client->setClientId('');
$client->setClientSecret('');
$client->setRedirectUri('URL/get_access_token.php');
$client->setScopes('https://www.googleapis.com/auth/analytics.readonly');
$client->setAccessType('offline');
if (isset($_GET['code'])) {
$client->authenticate();
$_SESSION['token'] = $client->getAccessToken();
$myFile = "refreshgatoken.conf";
$fh = fopen($myFile, 'w') or die("can't open file");
fwrite($fh, $client->getAccessToken());
fclose($fh);
} else {
if (!$client->getAccessToken()) {
$auth = $client->createAuthUrl();
header("Location: $auth");
}
}
Notice the refreshgatoken.conf is the name of the file being generated.
Google Requires for Google API:
require_once 'google-api-php-client/src/Google_Client.php';
require_once 'google-api-php-client/src/contrib/Google_AnalyticsService.php';
require_once 'google-api-php-client/src/contrib/Google_YouTubeAnalyticsService.php';
On these functions remember to setClientID, Secret, and RedirectUri to what is applicable to your server and Google API keys. Also ViewID and ChannelID are intentionally left out.
Example Google Analytics Function:
$viewId = "ga:xxxxxxx";
function OutboundClicksweek(){
$client = new Google_Client();
$client->setClientId('');
$client->setClientSecret('');
$client->setRedirectUri('URL/analytics.php');
$client->setScopes('https://www.googleapis.com/auth/analytics.readonly');
$client->setAccessToken(file_get_contents('refreshgatoken.conf'));
$client->setUseObjects(true);
$service = new Google_AnalyticsService($client);
$start_date = $GLOBALS["start_date"];
$end_date = new DateTime($start_date);
$end_date->add(new DateInterval('P6D'));
$end_date = $end_date->format('Y-m-d');
$metrics = "ga:totalEvents";
$dimensions = "ga:eventCategory";
$filters = "ga:eventCategory=~Outbound Traffic";
$optParams = array('dimensions' => $dimensions, 'filters' => $filters);
$props = $GLOBALS["service"]->data_ga->get($GLOBALS["viewId"],$start_date,$end_date,$metrics,$optParams);
$events=$props->totalsForAllResults['ga:totalEvents'];
return $events;
};
Example of YouTube Function:
$YTChannelID = "channel==xxxxxxxxx";
function YoutubeFacebookAnalytics(){
$client = new Google_Client();
$client->setClientId('');
$client->setClientSecret('');
$client->setRedirectUri('URL/analytics.php');
$client->setScopes(array('https://www.googleapis.com/auth/yt-analytics.readonly', 'https://www.googleapis.com/auth/youtube.readonly'));
$client->setAccessToken(file_get_contents('refreshyttoken.conf'));
$client->setUseObjects(true);
$service = new Google_YouTubeAnalyticsService($client);
$start_date = $GLOBALS["start_date"];
$end_date = new DateTime($start_date);
$end_date->add(new DateInterval('P6D'));
$end_date = $end_date->format('Y-m-d');
$metrics = "views";
$dimensions = "insightTrafficSourceDetail";
$filters = "insightTrafficSourceType==EXT_URL";
$max_results = "25";
$sort = "-views";
$optParams = array('dimensions' => $dimensions, 'filters' => $filters, 'max-results' => $max_results, 'sort' => $sort);
$pages = $service->reports->query($GLOBALS['YTChannelID'], $start_date, $end_date, $metrics, $optParams);
print_r($pages);
};
Once the coding is complete, simply go to the get access token php files in your browser and login to the appropriate Google Accounts. Then go back to your original page and you should be good to go.