|
12 | 12 |
|
13 | 13 |
|
14 | 14 | // Call set_include_path() as needed to point to your client library. |
15 | | -require_once 'Google_Client.php'; |
16 | | -require_once 'contrib/Google_YouTubeService.php'; |
| 15 | +require_once 'Google/Client.php'; |
| 16 | +require_once 'Google/Service/YouTube.php'; |
17 | 17 | session_start(); |
18 | 18 |
|
19 | 19 | /* |
|
29 | 29 | $client = new Google_Client(); |
30 | 30 | $client->setClientId($OAUTH2_CLIENT_ID); |
31 | 31 | $client->setClientSecret($OAUTH2_CLIENT_SECRET); |
| 32 | +$client->setScopes('https://www.googleapis.com/auth/youtube'); |
32 | 33 | $redirect = filter_var('http://' . $_SERVER['HTTP_HOST'] . $_SERVER['PHP_SELF'], |
33 | 34 | FILTER_SANITIZE_URL); |
34 | 35 | $client->setRedirectUri($redirect); |
35 | 36 |
|
36 | 37 | // Define an object that will be used to make all API requests. |
37 | | -$youtube = new Google_YoutubeService($client); |
| 38 | +$youtube = new Google_Service_YouTube($client); |
38 | 39 |
|
39 | 40 | if (isset($_GET['code'])) { |
40 | 41 | if (strval($_SESSION['state']) !== strval($_GET['state'])) { |
41 | 42 | die('The session state did not match.'); |
42 | 43 | } |
43 | 44 |
|
44 | | - $client->authenticate(); |
| 45 | + $client->authenticate($_GET['code']); |
45 | 46 | $_SESSION['token'] = $client->getAccessToken(); |
46 | 47 | header('Location: ' . $redirect); |
47 | 48 | } |
|
65 | 66 | // value for better recovery on less reliable connections. |
66 | 67 | $chunkSizeBytes = 1 * 1024 * 1024; |
67 | 68 |
|
68 | | - // Create a MediaFileUpload object for resumable uploads. |
69 | | - $media = new Google_MediaFileUpload('image/png', null, true, $chunkSizeBytes); |
70 | | - $media->setFileSize(filesize($imagePath)); |
| 69 | + // Setting the defer flag to true tells the client to return a request which can be called |
| 70 | + // with ->execute(); instead of making the API call immediately. |
| 71 | + $client->setDefer(true); |
71 | 72 |
|
72 | | - // Call the API's thumbnails.set method to upload the image and associate |
| 73 | + // Create a request for the API's thumbnails.set method to upload the image and associate |
73 | 74 | // it with the appropriate video. |
74 | | - $setResponse = $youtube->thumbnails->set($videoId, array('mediaUpload' => $media)); |
| 75 | + $setRequest = $youtube->thumbnails->set($videoId); |
75 | 76 |
|
76 | | - $uploadStatus = false; |
| 77 | + // Create a MediaFileUpload object for resumable uploads. |
| 78 | + $media = new Google_Http_MediaFileUpload( |
| 79 | + $client, |
| 80 | + $setRequest, |
| 81 | + 'image/png', |
| 82 | + null, |
| 83 | + true, |
| 84 | + $chunkSizeBytes |
| 85 | + ); |
| 86 | + $media->setFileSize(filesize($imagePath)); |
77 | 87 |
|
78 | | - // Read the image file and upload it chunk by chunk. |
| 88 | + |
| 89 | + // Read the media file and upload it chunk by chunk. |
| 90 | + $status = false; |
79 | 91 | $handle = fopen($imagePath, "rb"); |
80 | | - while (!$uploadStatus && !feof($handle)) { |
| 92 | + while (!$status && !feof($handle)) { |
81 | 93 | $chunk = fread($handle, $chunkSizeBytes); |
82 | | - $uploadStatus = $media->nextChunk($setResponse, $chunk); |
| 94 | + $status = $media->nextChunk($chunk); |
83 | 95 | } |
84 | 96 |
|
85 | 97 | fclose($handle); |
86 | 98 |
|
87 | | - $thumbnailUrl = $uploadStatus['items'][0]['default']['url']; |
| 99 | + // If you want to make other calls after the file upload, set setDefer back to false |
| 100 | + $client->setDefer(false); |
| 101 | + |
| 102 | + |
| 103 | + $thumbnailUrl = $status['items'][0]['default']['url']; |
88 | 104 | $htmlBody .= "<h3>Thumbnail Uploaded</h3><ul>"; |
89 | 105 | $htmlBody .= sprintf('<li>%s (%s)</li>', |
90 | 106 | $videoId, |
|
0 commit comments