Skip to content

Commit e552ab5

Browse files
"Updating samples to reflect recent changes."
1 parent 61ad3ae commit e552ab5

File tree

1 file changed

+14
-20
lines changed

1 file changed

+14
-20
lines changed

php/update_video.php

Lines changed: 14 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,8 @@
1212
*/
1313

1414
// 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';
1717
session_start();
1818

1919
/*
@@ -23,25 +23,26 @@
2323
* <https://developers.google.com/youtube/v3/guides/authentication>
2424
* Please ensure that you have enabled the YouTube Data API for your project.
2525
*/
26-
$OAUTH2_CLIENT_ID = 'REPLACE ME';
27-
$OAUTH2_CLIENT_SECRET = 'REPLACE ME';
26+
$OAUTH2_CLIENT_ID = 'REPLACE_ME';
27+
$OAUTH2_CLIENT_SECRET = 'REPLACE_ME';
2828

2929
$client = new Google_Client();
3030
$client->setClientId($OAUTH2_CLIENT_ID);
3131
$client->setClientSecret($OAUTH2_CLIENT_SECRET);
32+
$client->setScopes('https://www.googleapis.com/auth/youtube');
3233
$redirect = filter_var('http://' . $_SERVER['HTTP_HOST'] . $_SERVER['PHP_SELF'],
3334
FILTER_SANITIZE_URL);
3435
$client->setRedirectUri($redirect);
3536

3637
// 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);
3839

3940
if (isset($_GET['code'])) {
4041
if (strval($_SESSION['state']) !== strval($_GET['state'])) {
4142
die('The session state did not match.');
4243
}
4344

44-
$client->authenticate();
45+
$client->authenticate($_GET['code']);
4546
$_SESSION['token'] = $client->getAccessToken();
4647
header('Location: ' . $redirect);
4748
}
@@ -61,17 +62,14 @@
6162
$listResponse = $youtube->videos->listVideos("snippet",
6263
array('id' => $videoId));
6364

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)) {
6867
$htmlBody .= sprintf('<h3>Can\'t find a video with video id: %s</h3>', $videoId);
6968
} else {
7069
// Since the request specified a video ID, the response only
7170
// contains one video resource.
72-
$video = $videoList[0];
71+
$video = $listResponse[0];
7372
$videoSnippet = $video['snippet'];
74-
7573
$tags = $videoSnippet['tags'];
7674

7775
// Preserve any tags already associated with the video. If the video does
@@ -83,23 +81,19 @@
8381
array_push($tags, "tag1", "tag2");
8482
}
8583

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;
9286

9387
// Update the video resource by calling the videos.update() method.
94-
$updateResponse = $youtube->videos->update("snippet", $updateVideo);
88+
$updateResponse = $youtube->videos->update("snippet", $video);
9589

9690
$responseTags = $updateResponse['snippet']['tags'];
9791

9892

9993
$htmlBody .= "<h3>Video Updated</h3><ul>";
10094
$htmlBody .= sprintf('<li>Tags "%s" and "%s" added for video %s (%s) </li>',
10195
array_pop($responseTags), array_pop($responseTags),
102-
$videoId, $updateResponse['snippet']['title']);
96+
$videoId, $video['snippet']['title']);
10397

10498
$htmlBody .= '</ul>';
10599
}

0 commit comments

Comments
 (0)