|
| 1 | +<?php |
| 2 | + |
| 3 | +/** |
| 4 | + * This sample shuffles user's existing channel sections by: |
| 5 | + * |
| 6 | + * 1. Getting the active user's channel sections via "channelSections.list" method. |
| 7 | + * 2. Shuffling channel sections offline. |
| 8 | + * 3. Saving the newly shuffled channel sections list via the "channelSections.update" method. |
| 9 | + * |
| 10 | + * @author Ibrahim Ulukaya |
| 11 | +*/ |
| 12 | + |
| 13 | + |
| 14 | +// Call set_include_path() as needed to point to your client library. |
| 15 | +require_once 'Google/Client.php'; |
| 16 | +require_once 'Google/Service/YouTube.php'; |
| 17 | +session_start(); |
| 18 | + |
| 19 | +/* |
| 20 | + * You can acquire an OAuth 2.0 client ID and client secret from the |
| 21 | + * {{ Google Cloud Console }} <{{ https://cloud.google.com/console }}> |
| 22 | + * For more information about using OAuth 2.0 to access Google APIs, please see: |
| 23 | + * <https://developers.google.com/youtube/v3/guides/authentication> |
| 24 | + * Please ensure that you have enabled the YouTube Data API for your project. |
| 25 | + */ |
| 26 | +$OAUTH2_CLIENT_ID = 'REPLACE_ME'; |
| 27 | +$OAUTH2_CLIENT_SECRET = 'REPLACE_ME'; |
| 28 | + |
| 29 | +$client = new Google_Client(); |
| 30 | +$client->setClientId($OAUTH2_CLIENT_ID); |
| 31 | +$client->setClientSecret($OAUTH2_CLIENT_SECRET); |
| 32 | +$client->setScopes('https://www.googleapis.com/auth/youtube'); |
| 33 | +$redirect = filter_var('http://' . $_SERVER['HTTP_HOST'] . $_SERVER['PHP_SELF'], |
| 34 | + FILTER_SANITIZE_URL); |
| 35 | +$client->setRedirectUri($redirect); |
| 36 | + |
| 37 | +// Define an object that will be used to make all API requests. |
| 38 | +$youtube = new Google_Service_YouTube($client); |
| 39 | + |
| 40 | +if (isset($_GET['code'])) { |
| 41 | + if (strval($_SESSION['state']) !== strval($_GET['state'])) { |
| 42 | + die('The session state did not match.'); |
| 43 | + } |
| 44 | + |
| 45 | + $client->authenticate($_GET['code']); |
| 46 | + $_SESSION['token'] = $client->getAccessToken(); |
| 47 | + header('Location: ' . $redirect); |
| 48 | +} |
| 49 | + |
| 50 | +if (isset($_SESSION['token'])) { |
| 51 | + $client->setAccessToken($_SESSION['token']); |
| 52 | +} |
| 53 | + |
| 54 | +// Check to ensure that the access token was successfully acquired. |
| 55 | +if ($client->getAccessToken()) { |
| 56 | + try { |
| 57 | + |
| 58 | + // Call the YouTube Data API's channelSections.list method to retrieve your channel sections. |
| 59 | + $listResponse = $youtube->channelSections->listChannelSections('snippet,contentDetails', array('mine' => true)); |
| 60 | + $channelSections = $listResponse['items']; |
| 61 | + |
| 62 | + // This will randomly reorder the items in the channel_sections list. |
| 63 | + shuffle($channelSections); |
| 64 | + |
| 65 | + $htmlBody .= "<h2>Sections Shuffled</h2><ul>"; |
| 66 | + |
| 67 | + foreach ($channelSections as $channelSection) { |
| 68 | + // Each section in the list of shuffled sections is sequentially |
| 69 | + // set to position 0, i.e. the top. |
| 70 | + $channelSection['snippet']['position'] = 0; |
| 71 | + |
| 72 | + // Call the YouTube Data API's channelSections.update method to update a channel section. |
| 73 | + $updateResponse = $youtube->channelSections->update('snippet,contentDetails', $channelSection); |
| 74 | + |
| 75 | + $htmlBody .= sprintf('<li>%s "%s"</li>', |
| 76 | + $updateResponse['id'], $updateResponse['snippet']['title']); |
| 77 | + } |
| 78 | + |
| 79 | + $htmlBody .= '</ul>'; |
| 80 | + |
| 81 | + } catch (Google_ServiceException $e) { |
| 82 | + $htmlBody .= sprintf('<p>A service error occurred: <code>%s</code></p>', |
| 83 | + htmlspecialchars($e->getMessage())); |
| 84 | + } catch (Google_Exception $e) { |
| 85 | + $htmlBody .= sprintf('<p>An client error occurred: <code>%s</code></p>', |
| 86 | + htmlspecialchars($e->getMessage())); |
| 87 | + } |
| 88 | + |
| 89 | + $_SESSION['token'] = $client->getAccessToken(); |
| 90 | +} else { |
| 91 | + // If the user hasn't authorized the app, initiate the OAuth flow |
| 92 | + $state = mt_rand(); |
| 93 | + $client->setState($state); |
| 94 | + $_SESSION['state'] = $state; |
| 95 | + |
| 96 | + $authUrl = $client->createAuthUrl(); |
| 97 | + $htmlBody = <<<END |
| 98 | + <h3>Authorization Required</h3> |
| 99 | + <p>You need to <a href="$authUrl">authorize access</a> before proceeding.<p> |
| 100 | +END; |
| 101 | + } |
| 102 | + ?> |
| 103 | + |
| 104 | + <!doctype html> |
| 105 | + <html> |
| 106 | + <head> |
| 107 | + <title>Sections Shuffled</title> |
| 108 | + </head> |
| 109 | + <body> |
| 110 | + <?=$htmlBody?> |
| 111 | + </body> |
| 112 | + </html> |
0 commit comments