forked from youtube/api-samples
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathadd_channel_section.php
More file actions
164 lines (134 loc) · 6 KB
/
add_channel_section.php
File metadata and controls
164 lines (134 loc) · 6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
<?php
/**
* This sample creates a channel section by :
*
* 1. Getting the active user's channel branding settings via "channels.list" method.
* 2. Updating the active user's channel to show "browse view" via "channel.update" method.
* 3. Creating a channel section in the active user's channel via "channelSections->insert" method.
*
* @author Ibrahim Ulukaya
*/
// Call set_include_path() as needed to point to your client library.
require_once 'Google/Client.php';
require_once 'Google/Service/YouTube.php';
session_start();
// Valid section types.
$SECTION_TYPES = array("allPlaylists", "completedEvents", "likedPlaylists",
"likes", "liveEvents", "multipleChannels", "multiplePlaylists",
"popularUploads", "recentActivity", "recentPosts", "recentUploads",
"singlePlaylist", "upcomingEvents");
// Valid section styles.
$SECTION_STYLES = array("horizontalRow", "verticalList");
// Replace with section title of your choice.
$SECTION_TITLE = 'YOUR_SECTION_TITLE';
// The section's position on the channel page. This property uses a 0-based index.
// If you do not specify a value for this property when inserting a channel section,
// the default behavior is to display the new section last.
$SECTION_POSITION = 0;
// A list of playlist IDs that will be featured in a channel section.
$PLAYLISTS = '';
// A list of channel IDs that will be featured in a channel section.
$CHANNELS = '';
/*
* You can acquire an OAuth 2.0 client ID and client secret from the
* {{ Google Cloud Console }} <{{ https://cloud.google.com/console }}>
* For more information about using OAuth 2.0 to access Google APIs, please see:
* <https://developers.google.com/youtube/v3/guides/authentication>
* Please ensure that you have enabled the YouTube Data API for your project.
*/
$OAUTH2_CLIENT_ID = 'REPLACE_ME';
$OAUTH2_CLIENT_SECRET = 'REPLACE_ME';
$client = new Google_Client();
$client->setClientId($OAUTH2_CLIENT_ID);
$client->setClientSecret($OAUTH2_CLIENT_SECRET);
$client->setScopes('https://www.googleapis.com/auth/youtube');
$redirect = filter_var('http://' . $_SERVER['HTTP_HOST'] . $_SERVER['PHP_SELF'],
FILTER_SANITIZE_URL);
$client->setRedirectUri($redirect);
// Define an object that will be used to make all API requests.
$youtube = new Google_Service_YouTube($client);
if (isset($_GET['code'])) {
if (strval($_SESSION['state']) !== strval($_GET['state'])) {
die('The session state did not match.');
}
$client->authenticate($_GET['code']);
$_SESSION['token'] = $client->getAccessToken();
header('Location: ' . $redirect);
}
if (isset($_SESSION['token'])) {
$client->setAccessToken($_SESSION['token']);
}
// Check to ensure that the access token was successfully acquired.
if ($client->getAccessToken()) {
try {
/*
* Before channel shelves will appear on your channel's web page, browse
* view needs to be enabled. If you know that your channel already has
* it enabled, or if you want to add a number of sections before enabling it,
* you can skip the call to enable_browse_view().
*/
// Call the YouTube Data API's channels.list method to retrieve your channel.
$listResponse = $youtube->channels->listChannels('brandingSettings', array('mine' => true));
$channel = $listResponse['items'][0];
$channel['brandingSettings']['channel']['showBrowseView'] = true;
// Call the YouTube Data API's channels.update method to update your channel.
$updateResponse = $youtube->channels->update('brandingSettings', $channel);
// Create a channel section snippet object with type, style, title and position.
$channelSectionSnippet = new Google_Service_YouTube_ChannelSectionSnippet();
$channelSectionSnippet->setType($SECTION_TYPES[0]);
$channelSectionSnippet->setStyle($SECTION_STYLES[0]);
$channelSectionSnippet->setTitle($SECTION_TITLE);
$channelSectionSnippet->setPosition($SECTION_POSITION);
// Create a channel section contentDetails object with channels and playlists.
$channelSectionContentDetails = new Google_Service_YouTube_ChannelSectionContentDetails();
$channelSectionContentDetails->setChannels($CHANNELS);
$channelSectionContentDetails->setPlaylists($PLAYLISTS);
// Create a channel section with snippet and contentDetails.
$channelSection = new Google_Service_YouTube_ChannelSection();
$channelSection->setSnippet($channelSectionSnippet);
$channelSection->setContentDetails($channelSectionContentDetails);
// Call the YouTube Data API's channelSections.insert method to create a channel section.
$insertResponse = $youtube->channelSections->insert('snippet,contentDetails', $channelSection);
$htmlBody .= "<h2>Section Created</h2><ul>";
$htmlBody .= sprintf('<li>%s "%s"</li>',
$insertResponse['id'], $insertResponse['snippet']['title']);
$htmlBody .= '</ul>';
$htmlBody .= "<h3>with channels</h3><ul>";
foreach ($insertResponse['contentDetails']['channels'] as $channel) {
$channels .= sprintf('<li>%s</li>', $channel);
}
$htmlBody .= '</ul>';
$htmlBody .= "<h3>with playlists</h3><ul>";
foreach ($insertResponse['contentDetails']['playlists'] as $playlist) {
$channels .= sprintf('<li>%s</li>', $playlist);
}
$htmlBody .= '</ul>';
} catch (Google_Service_Exception $e) {
$htmlBody .= sprintf('<p>A service error occurred: <code>%s</code></p>',
htmlspecialchars($e->getMessage()));
} catch (Google_Exception $e) {
$htmlBody .= sprintf('<p>An client error occurred: <code>%s</code></p>',
htmlspecialchars($e->getMessage()));
}
$_SESSION['token'] = $client->getAccessToken();
} else {
// If the user hasn't authorized the app, initiate the OAuth flow
$state = mt_rand();
$client->setState($state);
$_SESSION['state'] = $state;
$authUrl = $client->createAuthUrl();
$htmlBody = <<<END
<h3>Authorization Required</h3>
<p>You need to <a href="$authUrl">authorize access</a> before proceeding.<p>
END;
}
?>
<!doctype html>
<html>
<head>
<title>Section Created</title>
</head>
<body>
<?=$htmlBody?>
</body>
</html>