|
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 | /* |
|
23 | 23 | * <https://developers.google.com/youtube/v3/guides/authentication> |
24 | 24 | * Please ensure that you have enabled the YouTube Data API for your project. |
25 | 25 | */ |
26 | | -$OAUTH2_CLIENT_ID = 'REPLACE ME'; |
27 | | -$OAUTH2_CLIENT_SECRET = 'REPLACE ME'; |
| 26 | +$OAUTH2_CLIENT_ID = 'REPLACE_ME'; |
| 27 | +$OAUTH2_CLIENT_SECRET = 'REPLACE_ME'; |
28 | 28 |
|
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 | } |
|
61 | 62 | $listResponse = $youtube->videos->listVideos("snippet", |
62 | 63 | array('id' => $videoId)); |
63 | 64 |
|
64 | | - $videoList = $listResponse['items']; |
65 | | - |
66 | | - // If the videoList variable is empty, the specified video was not found. |
67 | | - if (empty($videoList)) { |
| 65 | + // If $listResponse is empty, the specified video was not found. |
| 66 | + if (empty($listResponse)) { |
68 | 67 | $htmlBody .= sprintf('<h3>Can\'t find a video with video id: %s</h3>', $videoId); |
69 | 68 | } else { |
70 | 69 | // Since the request specified a video ID, the response only |
71 | 70 | // contains one video resource. |
72 | | - $video = $videoList[0]; |
| 71 | + $video = $listResponse[0]; |
73 | 72 | $videoSnippet = $video['snippet']; |
74 | | - |
75 | 73 | $tags = $videoSnippet['tags']; |
76 | 74 |
|
77 | 75 | // Preserve any tags already associated with the video. If the video does |
|
83 | 81 | array_push($tags, "tag1", "tag2"); |
84 | 82 | } |
85 | 83 |
|
86 | | - // Construct the video resource, using the updated tags, to send in the |
87 | | - // videos.update API request. |
88 | | - $updateVideo = new Google_Video($video); |
89 | | - $updateSnippet = new Google_VideoSnippet($videoSnippet); |
90 | | - $updateSnippet->setTags($tags); |
91 | | - $updateVideo -> setSnippet($updateSnippet); |
| 84 | + // Set the tags array for the video snippet |
| 85 | + $videoSnippet['tags'] = $tags; |
92 | 86 |
|
93 | 87 | // Update the video resource by calling the videos.update() method. |
94 | | - $updateResponse = $youtube->videos->update("snippet", $updateVideo); |
| 88 | + $updateResponse = $youtube->videos->update("snippet", $video); |
95 | 89 |
|
96 | 90 | $responseTags = $updateResponse['snippet']['tags']; |
97 | 91 |
|
98 | 92 |
|
99 | 93 | $htmlBody .= "<h3>Video Updated</h3><ul>"; |
100 | 94 | $htmlBody .= sprintf('<li>Tags "%s" and "%s" added for video %s (%s) </li>', |
101 | 95 | array_pop($responseTags), array_pop($responseTags), |
102 | | - $videoId, $updateResponse['snippet']['title']); |
| 96 | + $videoId, $video['snippet']['title']); |
103 | 97 |
|
104 | 98 | $htmlBody .= '</ul>'; |
105 | 99 | } |
|
0 commit comments