|
| 1 | +<?php |
| 2 | + |
| 3 | + |
| 4 | +/** |
| 5 | + * This sample sets a custom banner for a user's channel by: |
| 6 | + * |
| 7 | + * 1. Uploading a banner image with "youtube.channelBanners.insert" method via resumable upload |
| 8 | + * 2. Getting user's channel object with "youtube.channels.list" method and "mine" parameter |
| 9 | + * 3. Updating channel's banner external URL with "youtube.channels.update" method |
| 10 | + * |
| 11 | + * @author Ibrahim Ulukaya |
| 12 | +*/ |
| 13 | + |
| 14 | + |
| 15 | +// Call set_include_path() as needed to point to your client library. |
| 16 | +require_once 'Google/Client.php'; |
| 17 | +require_once 'Google/Service/YouTube.php'; |
| 18 | +session_start(); |
| 19 | + |
| 20 | +/* |
| 21 | + * You can acquire an OAuth 2.0 client ID and client secret from the |
| 22 | + * {{ Google Cloud Console }} <{{ https://cloud.google.com/console }}> |
| 23 | + * For more information about using OAuth 2.0 to access Google APIs, please see: |
| 24 | + * <https://developers.google.com/youtube/v3/guides/authentication> |
| 25 | + * Please ensure that you have enabled the YouTube Data API for your project. |
| 26 | + */ |
| 27 | +$OAUTH2_CLIENT_ID = 'REPLACE_ME'; |
| 28 | +$OAUTH2_CLIENT_SECRET = 'REPLACE_ME'; |
| 29 | + |
| 30 | +$client = new Google_Client(); |
| 31 | +$client->setClientId($OAUTH2_CLIENT_ID); |
| 32 | +$client->setClientSecret($OAUTH2_CLIENT_SECRET); |
| 33 | +$client->setScopes('https://www.googleapis.com/auth/youtube'); |
| 34 | +$redirect = filter_var('http://' . $_SERVER['HTTP_HOST'] . $_SERVER['PHP_SELF'], |
| 35 | + FILTER_SANITIZE_URL); |
| 36 | +$client->setRedirectUri($redirect); |
| 37 | + |
| 38 | +// Define an object that will be used to make all API requests. |
| 39 | +$youtube = new Google_Service_YouTube($client); |
| 40 | + |
| 41 | +if (isset($_GET['code'])) { |
| 42 | + if (strval($_SESSION['state']) !== strval($_GET['state'])) { |
| 43 | + die('The session state did not match.'); |
| 44 | + } |
| 45 | + |
| 46 | + $client->authenticate($_GET['code']); |
| 47 | + $_SESSION['token'] = $client->getAccessToken(); |
| 48 | + header('Location: ' . $redirect); |
| 49 | +} |
| 50 | + |
| 51 | +if (isset($_SESSION['token'])) { |
| 52 | + $client->setAccessToken($_SESSION['token']); |
| 53 | +} |
| 54 | + |
| 55 | +// Check to ensure that the access token was successfully acquired. |
| 56 | +if ($client->getAccessToken()) { |
| 57 | + try{ |
| 58 | + |
| 59 | + // REPLACE with the path to your file that you want to upload for thumbnail |
| 60 | + $imagePath = "/path/to/file.jpg"; |
| 61 | + |
| 62 | + // Specify the size of each chunk of data, in bytes. Set a higher value for |
| 63 | + // reliable connection as fewer chunks lead to faster uploads. Set a lower |
| 64 | + // value for better recovery on less reliable connections. |
| 65 | + $chunkSizeBytes = 1 * 1024 * 1024; |
| 66 | + |
| 67 | + // Setting the defer flag to true tells the client to return a request which can be called |
| 68 | + // with ->execute(); instead of making the API call immediately. |
| 69 | + $client->setDefer(true); |
| 70 | + |
| 71 | + $chan = new Google_Service_YouTube_ChannelBannerResource(); |
| 72 | + |
| 73 | + // Create a request for the API's channelBanners.insert method to upload the banner. |
| 74 | + $insertRequest = $youtube->channelBanners->insert($chan); |
| 75 | + |
| 76 | + // Create a MediaFileUpload object for resumable uploads. |
| 77 | + $media = new Google_Http_MediaFileUpload( |
| 78 | + $client, |
| 79 | + $insertRequest, |
| 80 | + 'image/jpeg', |
| 81 | + null, |
| 82 | + true, |
| 83 | + $chunkSizeBytes |
| 84 | + ); |
| 85 | + $media->setFileSize(filesize($imagePath)); |
| 86 | + |
| 87 | + |
| 88 | + // Read the media file and upload it chunk by chunk. |
| 89 | + $status = false; |
| 90 | + $handle = fopen($videoPath, "rb"); |
| 91 | + while (!$status && !feof($handle)) { |
| 92 | + $chunk = fread($handle, $chunkSizeBytes); |
| 93 | + $status = $media->nextChunk($chunk); |
| 94 | + } |
| 95 | + |
| 96 | + fclose($handle); |
| 97 | + |
| 98 | + // If you want to make other calls after the file upload, set setDefer back to false |
| 99 | + $client->setDefer(false); |
| 100 | + |
| 101 | + $thumbnailUrl = $status['url']; |
| 102 | + |
| 103 | + // Call the API's channels.list method with mine parameter to fetch authorized user's channel. |
| 104 | + $listResponse = $youtube->channels->listChannels('brandingSettings', array( |
| 105 | + 'mine' => 'true', |
| 106 | + )); |
| 107 | + |
| 108 | + $responseChannel = $listResponse[0]; |
| 109 | + $responseChannel['brandingSettings']['image']['bannerExternalUrl']=$thumbnailUrl; |
| 110 | + |
| 111 | + // Call the API's channels.update method to update branding settings of the channel. |
| 112 | + $updateResponse = $youtube->channels->update('brandingSettings', $responseChannel); |
| 113 | + |
| 114 | + $bannerMobileUrl = $updateResponse["brandingSettings"]["image"]["bannerMobileImageUrl"]; |
| 115 | + |
| 116 | + $htmlBody .= "<h3>Thumbnail Uploaded</h3><ul>"; |
| 117 | + $htmlBody .= sprintf('<li>%s</li>', |
| 118 | + $thumbnailUrl); |
| 119 | + $htmlBody .= sprintf('<img src="%s">', $bannerMobileUrl); |
| 120 | + $htmlBody .= '</ul>'; |
| 121 | + |
| 122 | + } catch (Google_ServiceException $e) { |
| 123 | + $htmlBody .= sprintf('<p>A service error occurred: <code>%s</code></p>', |
| 124 | + htmlspecialchars($e->getMessage())); |
| 125 | + } catch (Google_Exception $e) { |
| 126 | + $htmlBody .= sprintf('<p>An client error occurred: <code>%s</code></p>', |
| 127 | + htmlspecialchars($e->getMessage())); |
| 128 | + } |
| 129 | + |
| 130 | + $_SESSION['token'] = $client->getAccessToken(); |
| 131 | + } else { |
| 132 | + // If the user hasn't authorized the app, initiate the OAuth flow |
| 133 | + $state = mt_rand(); |
| 134 | + $client->setState($state); |
| 135 | + $_SESSION['state'] = $state; |
| 136 | + |
| 137 | + $authUrl = $client->createAuthUrl(); |
| 138 | + $htmlBody = <<<END |
| 139 | + <h3>Authorization Required</h3> |
| 140 | + <p>You need to <a href="$authUrl">authorize access</a> before proceeding.<p> |
| 141 | +END; |
| 142 | + } |
| 143 | + ?> |
| 144 | + |
| 145 | + <!doctype html> |
| 146 | + <html> |
| 147 | + <head> |
| 148 | + <title>Banner Uploaded and Set</title> |
| 149 | + </head> |
| 150 | + <body> |
| 151 | + <?=$htmlBody?> |
| 152 | + </body> |
| 153 | + </html> |
0 commit comments