|
| 1 | +<?php |
| 2 | + |
| 3 | +/** |
| 4 | + * This sample retrieves reports created by a specific job by: |
| 5 | + * |
| 6 | + * 1. Listing the jobs using the "jobs.list" method. |
| 7 | + * 2. Retrieving reports using the "reports.list" method. |
| 8 | + * |
| 9 | + * @author Ibrahim Ulukaya |
| 10 | + */ |
| 11 | + |
| 12 | +// Call set_include_path() as needed to point to your client library. |
| 13 | +require_once 'Google/Client.php'; |
| 14 | +require_once 'Google/Service/YouTubeReporting.php'; |
| 15 | +session_start(); |
| 16 | + |
| 17 | + |
| 18 | +/* |
| 19 | + * You can acquire an OAuth 2.0 client ID and client secret from the |
| 20 | + * {{ Google Cloud Console }} <{{ https://cloud.google.com/console }}> |
| 21 | + * For more information about using OAuth 2.0 to access Google APIs, please see: |
| 22 | + * <https://developers.google.com/youtube/v3/guides/authentication> |
| 23 | + * Please ensure that you have enabled the YouTube Data API for your project. |
| 24 | + */ |
| 25 | +$OAUTH2_CLIENT_ID = 'REPLACE_ME'; |
| 26 | +$OAUTH2_CLIENT_SECRET = 'REPLACE_ME'; |
| 27 | + |
| 28 | +$client = new Google_Client(); |
| 29 | +$client->setClientId($OAUTH2_CLIENT_ID); |
| 30 | +$client->setClientSecret($OAUTH2_CLIENT_SECRET); |
| 31 | + |
| 32 | +/* |
| 33 | + * This OAuth 2.0 access scope allows for full read/write access to the |
| 34 | + * authenticated user's account. |
| 35 | + */ |
| 36 | +$client->setScopes('https://www.googleapis.com/auth/yt-analytics-monetary.readonly'); |
| 37 | +$redirect = filter_var('http://' . $_SERVER['HTTP_HOST'] . $_SERVER['PHP_SELF'], |
| 38 | + FILTER_SANITIZE_URL); |
| 39 | +$client->setRedirectUri($redirect); |
| 40 | + |
| 41 | +// YouTube Reporting object used to make YouTube Reporting API requests. |
| 42 | +$youtubeReporting = new Google_Service_YoutubeReporting($client); |
| 43 | + |
| 44 | +if (isset($_GET['code'])) { |
| 45 | + if (strval($_SESSION['state']) !== strval($_GET['state'])) { |
| 46 | + die('The session state did not match.'); |
| 47 | + } |
| 48 | + |
| 49 | + $client->authenticate($_GET['code']); |
| 50 | + $_SESSION['token'] = $client->getAccessToken(); |
| 51 | + header('Location: ' . $redirect); |
| 52 | +} |
| 53 | + |
| 54 | +if (isset($_SESSION['token'])) { |
| 55 | + $client->setAccessToken($_SESSION['token']); |
| 56 | +} |
| 57 | + |
| 58 | +// Check to ensure that the access token was successfully acquired. |
| 59 | +if ($client->getAccessToken()) { |
| 60 | + try { |
| 61 | + if (empty(listReportingJobs($youtubeReporting, $htmlBody))) { |
| 62 | + $htmlBody .= sprintf('<p>No jobs found.</p>'); |
| 63 | + } else if ($_GET['reportUrl']){ |
| 64 | + downloadReport($youtubeReporting, $_GET['reportUrl'], $htmlBody); |
| 65 | + } else if ($_GET['jobId']){ |
| 66 | + retrieveReports($youtubeReporting, $_GET['jobId'], $htmlBody); |
| 67 | + } |
| 68 | + } catch (Google_Service_Exception $e) { |
| 69 | + $htmlBody .= sprintf('<p>A service error occurred: <code>%s</code></p>', |
| 70 | + htmlspecialchars($e->getMessage())); |
| 71 | + } catch (Google_Exception $e) { |
| 72 | + $htmlBody .= sprintf('<p>An client error occurred: <code>%s</code></p>', |
| 73 | + htmlspecialchars($e->getMessage())); |
| 74 | + } |
| 75 | + $_SESSION['token'] = $client->getAccessToken(); |
| 76 | +} else { |
| 77 | + // If the user hasn't authorized the app, initiate the OAuth flow |
| 78 | + $state = mt_rand(); |
| 79 | + $client->setState($state); |
| 80 | + $_SESSION['state'] = $state; |
| 81 | + |
| 82 | + $authUrl = $client->createAuthUrl(); |
| 83 | + $htmlBody = <<<END |
| 84 | + <h3>Authorization Required</h3> |
| 85 | + <p>You need to <a href="$authUrl">authorize access</a> before proceeding.<p> |
| 86 | +END; |
| 87 | +} |
| 88 | + |
| 89 | + |
| 90 | +/** |
| 91 | + * Returns a list of reporting jobs. (jobs.listJobs) |
| 92 | + * |
| 93 | + * @param Google_Service_YouTubereporting $youtubeReporting YouTube Reporting service object. |
| 94 | + * @param $htmlBody - html body. |
| 95 | + */ |
| 96 | +function listReportingJobs(Google_Service_YouTubeReporting $youtubeReporting, &$htmlBody) { |
| 97 | + // Call the YouTube Reporting API's jobs.list method to retrieve reporting jobs. |
| 98 | + $reportingJobs = $youtubeReporting->jobs->listJobs(); |
| 99 | + |
| 100 | + $htmlBody .= "<h3>Reporting Jobs</h3><ul>"; |
| 101 | + foreach ($reportingJobs as $job) { |
| 102 | + $htmlBody .= sprintf('<li>id: "%s", name: "%s" report type: "%s"</li>', $job['id'], |
| 103 | + $job['name'], $job['reportTypeId']); |
| 104 | + } |
| 105 | + $htmlBody .= '</ul>'; |
| 106 | + |
| 107 | + return $reportingJobs; |
| 108 | +} |
| 109 | + |
| 110 | + |
| 111 | +/** |
| 112 | + * Lists reports created by a specific job. (reports.listJobsReports) |
| 113 | + * |
| 114 | + * @param Google_Service_YouTubereporting $youtubeReporting YouTube Reporting service object. |
| 115 | + * @param string $jobId The ID of the job. |
| 116 | + * @param $htmlBody - html body. |
| 117 | + */ |
| 118 | +function retrieveReports(Google_Service_YouTubeReporting $youtubeReporting, $jobId, &$htmlBody) { |
| 119 | + // Call the YouTube Reporting API's reports.list method to retrieve reports created by a job. |
| 120 | + $reports = $youtubeReporting->jobs_reports->listJobsReports($jobId); |
| 121 | + |
| 122 | + if (empty($reports)) { |
| 123 | + $htmlBody .= sprintf('<p>No reports found.</p>'); |
| 124 | + } else { |
| 125 | + $htmlBody .= sprintf('<h2>Reports for the job "%s"</h2><ul>', $jobId); |
| 126 | + foreach ($reports as $report) { |
| 127 | + $htmlBody .= sprintf('<li>From "%s" to "%s" downloadable at "%s"</li>', |
| 128 | + $report['startTime'], $report['endTime'], $report['downloadUrl']); |
| 129 | + $htmlBody .= '</ul>'; |
| 130 | + } |
| 131 | + } |
| 132 | +} |
| 133 | + |
| 134 | + |
| 135 | +/** |
| 136 | + * Download the report specified by the URL. (media.download) |
| 137 | + * |
| 138 | + * @param Google_Service_YouTubereporting $youtubeReporting YouTube Reporting service object. |
| 139 | + * @param string $reportUrl The URL of the report to be downloaded. |
| 140 | + * @param $htmlBody - html body. |
| 141 | + */ |
| 142 | +function downloadReport(Google_Service_YouTubeReporting $youtubeReporting, $reportUrl, &$htmlBody) { |
| 143 | + $client = $youtubeReporting->getClient(); |
| 144 | + // Setting the defer flag to true tells the client to return a request which can be called |
| 145 | + // with ->execute(); instead of making the API call immediately. |
| 146 | + $client->setDefer(true); |
| 147 | + |
| 148 | + // Call the YouTube Reporting API's media.download method to download a report. |
| 149 | + $request = $youtubeReporting->media->download(""); |
| 150 | + $request->setUrl($reportUrl); |
| 151 | + $response = $client->execute($request); |
| 152 | + |
| 153 | + file_put_contents("reportFile", $response->getResponseBody()); |
| 154 | + $client->setDefer(false); |
| 155 | +} |
| 156 | +?> |
| 157 | + |
| 158 | +<!doctype html> |
| 159 | +<html> |
| 160 | +<head> |
| 161 | +<title>Retrieve reports</title> |
| 162 | +</head> |
| 163 | +<body> |
| 164 | + <form method="GET"> |
| 165 | + <div> |
| 166 | + Job Id: <input type="text" id="jobId" name="jobId" placeholder="Enter Job Id"> |
| 167 | + </div> |
| 168 | + <br> |
| 169 | + <div> |
| 170 | + Report URL: <input type="text" id="reportUrl" name="reportUrl" placeholder="Enter Report Url"> |
| 171 | + </div> |
| 172 | + <br> <input type="submit" value="Retrieve!"> |
| 173 | + </form> |
| 174 | + <?=$htmlBody?> |
| 175 | +</body> |
| 176 | +</html> |
0 commit comments