|
| 1 | +<?php |
| 2 | + |
| 3 | +/** |
| 4 | + * This sample creates a reporting job by: |
| 5 | + * |
| 6 | + * 1. Listing the available report types using the "reportTypes.list" method. |
| 7 | + * 2. Creating a reporting job using the "jobs.create" 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 read access to the YouTube Analytics monetary reports for |
| 34 | + * authenticated user's account. Any request that retrieves earnings or ad performance metrics must |
| 35 | + * use this scope. |
| 36 | + */ |
| 37 | +$client->setScopes('https://www.googleapis.com/auth/yt-analytics-monetary.readonly'); |
| 38 | +$redirect = filter_var('http://' . $_SERVER['HTTP_HOST'] . $_SERVER['PHP_SELF'], |
| 39 | + FILTER_SANITIZE_URL); |
| 40 | +$client->setRedirectUri($redirect); |
| 41 | + |
| 42 | +// YouTube Reporting object used to make YouTube Reporting API requests. |
| 43 | +$youtubeReporting = new Google_Service_YouTubeReporting($client); |
| 44 | + |
| 45 | +if (isset($_GET['code'])) { |
| 46 | + if (strval($_SESSION['state']) !== strval($_GET['state'])) { |
| 47 | + die('The session state did not match.'); |
| 48 | + } |
| 49 | + |
| 50 | + $client->authenticate($_GET['code']); |
| 51 | + $_SESSION['token'] = $client->getAccessToken(); |
| 52 | + header('Location: ' . $redirect); |
| 53 | +} |
| 54 | + |
| 55 | +if (isset($_SESSION['token'])) { |
| 56 | + $client->setAccessToken($_SESSION['token']); |
| 57 | +} |
| 58 | + |
| 59 | +// Check to ensure that the access token was successfully acquired. |
| 60 | +if ($client->getAccessToken()) { |
| 61 | + // This code executes if the user enters a name in the form |
| 62 | + // and submits the form. Otherwise, the page displays the form above. |
| 63 | + try { |
| 64 | + if (empty(listReportTypes($youtubeReporting, $htmlBody))) { |
| 65 | + $htmlBody .= sprintf('<p>No report types found.</p>'); |
| 66 | + } else if ($_GET['reportTypeId']){ |
| 67 | + createReportingJob($youtubeReporting, $_GET['reportTypeId'], $_GET['jobName'], $htmlBody); |
| 68 | + } |
| 69 | + } catch (Google_Service_Exception $e) { |
| 70 | + $htmlBody .= sprintf('<p>A service error occurred: <code>%s</code></p>', |
| 71 | + htmlspecialchars($e->getMessage())); |
| 72 | + } catch (Google_Exception $e) { |
| 73 | + $htmlBody .= sprintf('<p>An client error occurred: <code>%s</code></p>', |
| 74 | + htmlspecialchars($e->getMessage())); |
| 75 | + } |
| 76 | + $_SESSION['token'] = $client->getAccessToken(); |
| 77 | +} else { |
| 78 | + // If the user hasn't authorized the app, initiate the OAuth flow |
| 79 | + $state = mt_rand(); |
| 80 | + $client->setState($state); |
| 81 | + $_SESSION['state'] = $state; |
| 82 | + |
| 83 | + $authUrl = $client->createAuthUrl(); |
| 84 | + $htmlBody = <<<END |
| 85 | + <h3>Authorization Required</h3> |
| 86 | + <p>You need to <a href="$authUrl">authorize access</a> before proceeding.<p> |
| 87 | +END; |
| 88 | +} |
| 89 | + |
| 90 | + |
| 91 | +/** |
| 92 | + * Creates a reporting job. (jobs.create) |
| 93 | + * |
| 94 | + * @param Google_Service_YouTubereporting $youtubeReporting YouTube Reporting service object. |
| 95 | + * @param string $reportTypeId Id of the job's report type. |
| 96 | + * @param string $name name of the job. |
| 97 | + * @param $htmlBody - html body. |
| 98 | + */ |
| 99 | +function createReportingJob(Google_Service_YouTubeReporting $youtubeReporting, $reportTypeId, |
| 100 | + $name, &$htmlBody) { |
| 101 | + # Create a reporting job with a name and a report type id. |
| 102 | + $reportingJob = new Google_Service_YouTubeReporting_Job(); |
| 103 | + $reportingJob->setReportTypeId($reportTypeId); |
| 104 | + $reportingJob->setName($name); |
| 105 | + |
| 106 | + // Call the YouTube Reporting API's jobs.create method to create a job. |
| 107 | + $jobCreateResponse = $youtubeReporting->jobs->create($reportingJob); |
| 108 | + |
| 109 | + $htmlBody .= "<h2>Created reporting job</h2><ul>"; |
| 110 | + $htmlBody .= sprintf('<li>"%s" for reporting type "%s" at "%s"</li>', |
| 111 | + $jobCreateResponse['name'], $jobCreateResponse['reportTypeId'], $jobCreateResponse['createTime']); |
| 112 | + $htmlBody .= '</ul>'; |
| 113 | +} |
| 114 | + |
| 115 | + |
| 116 | +/** |
| 117 | + * Returns a list of report types. (reportTypes.listReportTypes) |
| 118 | + * |
| 119 | + * @param Google_Service_YouTubereporting $youtubeReporting YouTube Reporting service object. |
| 120 | + * @param $htmlBody - html body. |
| 121 | + */ |
| 122 | +function listReportTypes(Google_Service_YouTubeReporting $youtubeReporting, &$htmlBody) { |
| 123 | + // Call the YouTube Reporting API's reportTypes.list method to retrieve report types. |
| 124 | + $reportTypes = $youtubeReporting->reportTypes->listReportTypes(); |
| 125 | + |
| 126 | + $htmlBody .= "<h3>Report Types</h3><ul>"; |
| 127 | + foreach ($reportTypes as $reportType) { |
| 128 | + $htmlBody .= sprintf('<li>id: "%s", name: "%s"</li>', $reportType['id'], $reportType['name']); |
| 129 | + } |
| 130 | + $htmlBody .= '</ul>'; |
| 131 | + |
| 132 | + return $reportTypes; |
| 133 | +} |
| 134 | +?> |
| 135 | + |
| 136 | +<!doctype html> |
| 137 | +<html> |
| 138 | +<head> |
| 139 | +<title>Create a reporting job</title> |
| 140 | +</head> |
| 141 | +<body> |
| 142 | + <form method="GET"> |
| 143 | + <div> |
| 144 | + Job Name: <input type="text" id="jobName" name="jobName" placeholder="Enter Job Name"> |
| 145 | + </div> |
| 146 | + <br> |
| 147 | + <div> |
| 148 | + Report Type Id: <input type="text" id="reportTypeId" name="reportTypeId" placeholder="Enter Report Type Id"> |
| 149 | + </div> |
| 150 | + <br> |
| 151 | + <input type="submit" value="Create!"> |
| 152 | + </form> |
| 153 | + <?=$htmlBody?> |
| 154 | +</body> |
| 155 | +</html> |
0 commit comments