Skip to content

Commit

Permalink
Update retrieve_reports.php
Browse files Browse the repository at this point in the history
Support command-line parameters to set content owner, job ID, download URL. Support option to include system-managed reports. Update auth flow.
  • Loading branch information
AndyDiamondstein committed Aug 16, 2017
1 parent 170530f commit 5193a5a
Showing 1 changed file with 130 additions and 129 deletions.
259 changes: 130 additions & 129 deletions php/retrieve_reports.php
@@ -1,12 +1,15 @@
<?php

/**
* This sample retrieves reports created by a specific job by:
* This sample supports the following use cases:
*
* 1. Listing the jobs using the "jobs.list" method.
* 2. Retrieving reports using the "reports.list" method.
*
* @author Ibrahim Ulukaya
* 1. Retrieve reporting jobs by content owner:
* Ex: php retrieve_reports.php --contentOwner=="CONTENT_OWNER_ID"
* Ex: php retrieve_reports.php --contentOwner=="CONTENT_OWNER_ID" --includeSystemManaged==True
* 2. Retrieving list of downloadable reports for a particular job:
* Ex: php retrieve_reports.php --contentOwner=="CONTENT_OWNER_ID" --jobId="JOB_ID"
* 3. Download a report:
* Ex: php retrieve_reports.php --contentOwner=="CONTENT_OWNER_ID" --downloadUrl="DOWNLOAD_URL" --outputFile="report.txt"
*/

/**
Expand All @@ -18,180 +21,178 @@
* $ composer require google/apiclient:~2.0
*/
if (!file_exists(__DIR__ . '/vendor/autoload.php')) {
throw new \Exception('please run "composer require google/apiclient:~2.0" in "' . __DIR__ .'"');
throw new \Exception('please run "composer require google/apiclient:~2.2.0" in "' . __DIR__ .'"');
}

require_once __DIR__ . '/vendor/autoload.php';
session_start();


define('CREDENTIALS_PATH', '~/.credentials/youtube-php.json');

$longOptions = array(
'contentOwner::',
'downloadUrl::',
'includeSystemManaged::',
'jobId::',
'outputFile::',
);

$options = getopt('', $longOptions);

$CONTENT_OWNER_ID = ($options['contentOwner'] ? $options['contentOwner'] : '');
$DOWNLOAD_URL = (array_key_exists('downloadUrl', $options) ?
$options['downloadUrl'] : '');
$INCLUDE_SYSTEM_MANAGED = (array_key_exists('includeSystemManaged', $options) ?
$options['includeSystemManaged'] : '');
$JOB_ID = (array_key_exists('jobId', $options) ? $options['jobId'] : '');
$OUTPUT_FILE = (array_key_exists('outputFile', $options) ?
$options['outputFile'] : '');

/*
* You can acquire an OAuth 2.0 client ID and client secret from the
* You can obtain an OAuth 2.0 client ID and client secret from the
* {{ Google Cloud Console }} <{{ https://cloud.google.com/console }}>
* For more information about using OAuth 2.0 to access Google APIs, please see:
* <https://developers.google.com/youtube/v3/guides/authentication>
* Please ensure that you have enabled the YouTube Data API for your project.
*/
$OAUTH2_CLIENT_ID = 'REPLACE_ME';
$OAUTH2_CLIENT_SECRET = 'REPLACE_ME';

$client = new Google_Client();
$client->setClientId($OAUTH2_CLIENT_ID);
$client->setClientSecret($OAUTH2_CLIENT_SECRET);

/*
* This OAuth 2.0 access scope allows for full read/write access to the
* authenticated user's account.
*/
$client->setScopes('https://www.googleapis.com/auth/yt-analytics-monetary.readonly');
$redirect = filter_var('http://' . $_SERVER['HTTP_HOST'] . $_SERVER['PHP_SELF'],
FILTER_SANITIZE_URL);
$client->setRedirectUri($redirect);

// YouTube Reporting object used to make YouTube Reporting API requests.
$youtubeReporting = new Google_Service_YoutubeReporting($client);
function getClient() {
$client = new Google_Client();
$client->setAuthConfigFile('client_secrets_php.json');
$client->addScope(
'https://www.googleapis.com/auth/yt-analytics-monetary.readonly');
$client->setRedirectUri('urn:ietf:wg:oauth:2.0:oob');
$client->setAccessType('offline');

// Load previously authorized credentials from a file.
$credentialsPath = expandHomeDirectory(CREDENTIALS_PATH);
if (file_exists($credentialsPath)) {
$accessToken = json_decode(file_get_contents($credentialsPath), true);
} else {
// Request authorization from the user.
$authUrl = $client->createAuthUrl();
printf('Open the following link in your browser:\n%s\n', $authUrl);
print 'Enter verification code: ';
$authCode = trim(fgets(STDIN));

// Exchange authorization code for an access token.
$accessToken = $client->authenticate($authCode);
$refreshToken = $client->getRefreshToken();

// Store the credentials to disk.
if(!file_exists(dirname($credentialsPath))) {
mkdir(dirname($credentialsPath), 0700, true);
}
file_put_contents($credentialsPath, json_encode($accessToken));
printf('Credentials saved to %s\n', $credentialsPath);

// Check if an auth token exists for the required scopes
$tokenSessionKey = 'token-' . $client->prepareScopes();
if (isset($_GET['code'])) {
if (strval($_SESSION['state']) !== strval($_GET['state'])) {
die('The session state did not match.');
//fclose($fp);
}
$client->setAccessToken($accessToken);

$client->authenticate($_GET['code']);
$_SESSION[$tokenSessionKey] = $client->getAccessToken();
header('Location: ' . $redirect);
}
// Refresh the token if it's expired.
if ($client->isAccessTokenExpired()) {
$client->fetchAccessTokenWithRefreshToken($client->getRefreshToken());
file_put_contents($credentialsPath, json_encode($client->getAccessToken()));
}

if (isset($_SESSION[$tokenSessionKey])) {
$client->setAccessToken($_SESSION[$tokenSessionKey]);
return $client;
}

// Check to ensure that the access token was successfully acquired.
if ($client->getAccessToken()) {
$htmlBody = '';
try {
if (empty(listReportingJobs($youtubeReporting, $htmlBody))) {
$htmlBody .= sprintf('<p>No jobs found.</p>');
} else if ($_GET['reportUrl']){
downloadReport($youtubeReporting, $_GET['reportUrl'], $htmlBody);
} else if ($_GET['jobId']){
retrieveReports($youtubeReporting, $_GET['jobId'], $htmlBody);
}
} catch (Google_Service_Exception $e) {
$htmlBody .= sprintf('<p>A service error occurred: <code>%s</code></p>',
htmlspecialchars($e->getMessage()));
} catch (Google_Exception $e) {
$htmlBody .= sprintf('<p>An client error occurred: <code>%s</code></p>',
htmlspecialchars($e->getMessage()));
/**
* Expands the home directory alias '~' to the full path.
* @param string $path the path to expand.
* @return string the expanded path.
*/
function expandHomeDirectory($path) {
$homeDirectory = getenv('HOME');
if (empty($homeDirectory)) {
$homeDirectory = getenv('HOMEDRIVE') . getenv('HOMEPATH');
}
$_SESSION[$tokenSessionKey] = $client->getAccessToken();
} elseif ($OAUTH2_CLIENT_ID == 'REPLACE_ME') {
$htmlBody = <<<END
<h3>Client Credentials Required</h3>
<p>
You need to set <code>\$OAUTH2_CLIENT_ID</code> and
<code>\$OAUTH2_CLIENT_ID</code> before proceeding.
<p>
END;
} else {
// If the user hasn't authorized the app, initiate the OAuth flow
$state = mt_rand();
$client->setState($state);
$_SESSION['state'] = $state;

$authUrl = $client->createAuthUrl();
$htmlBody = <<<END
<h3>Authorization Required</h3>
<p>You need to <a href="$authUrl">authorize access</a> before proceeding.<p>
END;
return str_replace('~', realpath($homeDirectory), $path);
}


/**
* Returns a list of reporting jobs. (jobs.listJobs)
*
* @param Google_Service_YouTubereporting $youtubeReporting YouTube Reporting service object.
* @param $htmlBody - html body.
* @param string $onBehalfOfContentOwner A content owner ID.
*/
function listReportingJobs(Google_Service_YouTubeReporting $youtubeReporting, &$htmlBody) {
// Call the YouTube Reporting API's jobs.list method to retrieve reporting jobs.
$reportingJobs = $youtubeReporting->jobs->listJobs();

$htmlBody .= "<h3>Reporting Jobs</h3><ul>";
function listReportingJobs(Google_Service_YouTubeReporting $youtubeReporting,
$onBehalfOfContentOwner = '', $includeSystemManaged = False) {
$reportingJobs = $youtubeReporting->jobs->listJobs(
array('onBehalfOfContentOwner' => $onBehalfOfContentOwner,
'includeSystemManaged' => $includeSystemManaged));
print ('REPORTING JOBS' . PHP_EOL . '**************' . PHP_EOL);
foreach ($reportingJobs as $job) {
$htmlBody .= sprintf('<li>id: "%s", name: "%s" report type: "%s"</li>', $job['id'],
$job['name'], $job['reportTypeId']);
print($job['reportTypeId'] . ':' . $job['id'] . PHP_EOL);
}
$htmlBody .= '</ul>';

return $reportingJobs;
print(PHP_EOL);
}


/**
* Lists reports created by a specific job. (reports.listJobsReports)
*
* @param Google_Service_YouTubereporting $youtubeReporting YouTube Reporting service object.
* @param string $jobId The ID of the job.
* @param $htmlBody - html body.
* @param string $onBehalfOfContentOwner A content owner ID.
*/
function retrieveReports(Google_Service_YouTubeReporting $youtubeReporting, $jobId, &$htmlBody) {
// Call the YouTube Reporting API's reports.list method to retrieve reports created by a job.
$reports = $youtubeReporting->jobs_reports->listJobsReports($jobId);

if (empty($reports)) {
$htmlBody .= sprintf('<p>No reports found.</p>');
} else {
$htmlBody .= sprintf('<h2>Reports for the job "%s"</h2><ul>', $jobId);
foreach ($reports as $report) {
$htmlBody .= sprintf('<li>From "%s" to "%s" downloadable at "%s"</li>',
$report['startTime'], $report['endTime'], $report['downloadUrl']);
$htmlBody .= '</ul>';
}
function listReportsForJob(Google_Service_YouTubeReporting $youtubeReporting,
$jobId, $onBehalfOfContentOwner = '') {
$reports = $youtubeReporting->jobs_reports->listJobsReports($jobId,
array('onBehalfOfContentOwner' => $onBehalfOfContentOwner));
print ('DOWNLOADABLE REPORTS' . PHP_EOL . '********************' . PHP_EOL);
foreach ($reports['reports'] as $report) {
print('Created: ' . date('d M Y', strtotime($report['createTime'])) .
' (' . date('d M Y', strtotime($report['startTime'])) .
' to ' . date('d M Y', strtotime($report['endTime'])) . ')' .
PHP_EOL . ' ' . $report['downloadUrl'] . PHP_EOL . PHP_EOL);
}
}


/**
* Download the report specified by the URL. (media.download)
*
* @param Google_Service_YouTubereporting $youtubeReporting YouTube Reporting service object.
* @param string $reportUrl The URL of the report to be downloaded.
* @param string $outputFile The file to write the report to locally.
* @param $htmlBody - html body.
*/
function downloadReport(Google_Service_YouTubeReporting $youtubeReporting, $reportUrl, &$htmlBody) {
function downloadReport(Google_Service_YouTubeReporting $youtubeReporting,
$reportUrl, $outputFile) {
$client = $youtubeReporting->getClient();
// Setting the defer flag to true tells the client to return a request which can be called
// with ->execute(); instead of making the API call immediately.
// Setting the defer flag to true tells the client to return a request that
// can be called with ->execute(); instead of making the API call immediately.
$client->setDefer(true);

// Call the YouTube Reporting API's media.download method to download a report.
$request = $youtubeReporting->media->download("", array("alt" => "media"));
// Call YouTube Reporting API's media.download method to download a report.
$request = $youtubeReporting->media->download('', array('alt' => 'media'));
$request = $request->withUri(new \GuzzleHttp\Psr7\Uri($reportUrl));
$response = $client->execute($request);

file_put_contents("reportFile", $response->getBody());
$responseBody = '';
try {
$response = $client->execute($request);
$responseBody = $response->getBody();
} catch (Google_Service_Exception $e) {
$responseBody = $e->getTrace()[0]['args'][0]->getResponseBody();
}
file_put_contents($outputFile, $responseBody);
$client->setDefer(false);
}
?>

<!doctype html>
<html>
<head>
<title>Retrieve reports</title>
</head>
<body>
<form method="GET">
<div>
Job Id: <input type="text" id="jobId" name="jobId" placeholder="Enter Job Id">
</div>
<br>
<div>
Report URL: <input type="text" id="reportUrl" name="reportUrl" placeholder="Enter Report Url">
</div>
<br> <input type="submit" value="Retrieve!">
</form>
<?=$htmlBody?>
</body>
</html>
// Define an object that will be used to make all API requests.
$client = getClient();
// YouTube Reporting object used to make YouTube Reporting API requests.
$youtubeReporting = new Google_Service_YouTubeReporting($client);

if ($CONTENT_OWNER_ID) {
if (!$DOWNLOAD_URL && !$JOB_ID) {
listReportingJobs($youtubeReporting, $CONTENT_OWNER_ID,
$INCLUDE_SYSTEM_MANAGED);
} else if ($JOB_ID) {
listReportsForJob($youtubeReporting, $JOB_ID, $CONTENT_OWNER_ID);
} else if ($DOWNLOAD_URL && $OUTPUT_FILE) {
downloadReport($youtubeReporting, $DOWNLOAD_URL, $OUTPUT_FILE);
}
}

?>

0 comments on commit 5193a5a

Please sign in to comment.