Skip to content

Commit dcc670a

Browse files
committed
updates comments to use google/apiclient:^2.0 and adds tests, better warnings, misc cleanup
1 parent edc8d3e commit dcc670a

26 files changed

+1133
-341
lines changed

php/.gitignore

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
vendor
2+
composer.*

php/add_channel_section.php

+29-10
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,19 @@
1010
* @author Ibrahim Ulukaya
1111
*/
1212

13+
/**
14+
* Library Requirements
15+
*
16+
* 1. Install composer (https://getcomposer.org)
17+
* 2. On the command line, change to this directory (api-samples/php)
18+
* 3. Require the google/apiclient library
19+
* $ composer require google/apiclient:~2.0
20+
*/
21+
if (!file_exists($file = __DIR__ . '/vendor/autoload.php')) {
22+
throw new \Exception('please run "composer require google/apiclient:~2.0" in "' . __DIR__ .'"');
23+
}
1324

14-
// Call set_include_path() as needed to point to your client library.
15-
require_once 'Google/Client.php';
16-
require_once 'Google/Service/YouTube.php';
25+
require_once __DIR__ . '/vendor/autoload.php';
1726
session_start();
1827

1928
// Valid section types.
@@ -60,18 +69,20 @@
6069
// Define an object that will be used to make all API requests.
6170
$youtube = new Google_Service_YouTube($client);
6271

72+
// Check if an auth token exists for the required scopes
73+
$tokenSessionKey = 'token-' . $client->prepareScopes();
6374
if (isset($_GET['code'])) {
6475
if (strval($_SESSION['state']) !== strval($_GET['state'])) {
6576
die('The session state did not match.');
6677
}
6778

6879
$client->authenticate($_GET['code']);
69-
$_SESSION['token'] = $client->getAccessToken();
80+
$_SESSION[$tokenSessionKey] = $client->getAccessToken();
7081
header('Location: ' . $redirect);
7182
}
7283

73-
if (isset($_SESSION['token'])) {
74-
$client->setAccessToken($_SESSION['token']);
84+
if (isset($_SESSION[$tokenSessionKey])) {
85+
$client->setAccessToken($_SESSION[$tokenSessionKey]);
7586
}
7687

7788
// Check to ensure that the access token was successfully acquired.
@@ -113,7 +124,7 @@
113124
// Call the YouTube Data API's channelSections.insert method to create a channel section.
114125
$insertResponse = $youtube->channelSections->insert('snippet,contentDetails', $channelSection);
115126

116-
$htmlBody .= "<h2>Section Created</h2><ul>";
127+
$htmlBody = "<h2>Section Created</h2><ul>";
117128
$htmlBody .= sprintf('<li>%s "%s"</li>',
118129
$insertResponse['id'], $insertResponse['snippet']['title']);
119130
$htmlBody .= '</ul>';
@@ -131,14 +142,22 @@
131142
$htmlBody .= '</ul>';
132143

133144
} catch (Google_Service_Exception $e) {
134-
$htmlBody .= sprintf('<p>A service error occurred: <code>%s</code></p>',
145+
$htmlBody = sprintf('<p>A service error occurred: <code>%s</code></p>',
135146
htmlspecialchars($e->getMessage()));
136147
} catch (Google_Exception $e) {
137-
$htmlBody .= sprintf('<p>An client error occurred: <code>%s</code></p>',
148+
$htmlBody = sprintf('<p>An client error occurred: <code>%s</code></p>',
138149
htmlspecialchars($e->getMessage()));
139150
}
140151

141-
$_SESSION['token'] = $client->getAccessToken();
152+
$_SESSION[$tokenSessionKey] = $client->getAccessToken();
153+
} elseif ($OAUTH2_CLIENT_ID == 'REPLACE_ME') {
154+
$htmlBody = <<<END
155+
<h3>Client Credentials Required</h3>
156+
<p>
157+
You need to set <code>\$OAUTH2_CLIENT_ID</code> and
158+
<code>\$OAUTH2_CLIENT_ID</code> before proceeding.
159+
<p>
160+
END;
142161
} else {
143162
// If the user hasn't authorized the app, initiate the OAuth flow
144163
$state = mt_rand();

php/add_subscription.php

+28-7
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,18 @@
11
<?php
22

3-
// Call set_include_path() as needed to point to your client library.
4-
require_once 'Google/Client.php';
5-
require_once 'Google/Service/YouTube.php';
3+
/**
4+
* Library Requirements
5+
*
6+
* 1. Install composer (https://getcomposer.org)
7+
* 2. On the command line, change to this directory (api-samples/php)
8+
* 3. Require the google/apiclient library
9+
* $ composer require google/apiclient:~2.0
10+
*/
11+
if (!file_exists(__DIR__ . '/vendor/autoload.php')) {
12+
throw new \Exception('please run "composer require google/apiclient:~2.0" in "' . __DIR__ .'"');
13+
}
14+
15+
require_once __DIR__ . '/vendor/autoload.php';
616
session_start();
717

818
/*
@@ -26,22 +36,25 @@
2636
// Define an object that will be used to make all API requests.
2737
$youtube = new Google_Service_YouTube($client);
2838

39+
// Check if an auth token exists for the required scopes
40+
$tokenSessionKey = 'token-' . $client->prepareScopes();
2941
if (isset($_GET['code'])) {
3042
if (strval($_SESSION['state']) !== strval($_GET['state'])) {
3143
die('The session state did not match.');
3244
}
3345

3446
$client->authenticate($_GET['code']);
35-
$_SESSION['token'] = $client->getAccessToken();
47+
$_SESSION[$tokenSessionKey] = $client->getAccessToken();
3648
header('Location: ' . $redirect);
3749
}
3850

39-
if (isset($_SESSION['token'])) {
40-
$client->setAccessToken($_SESSION['token']);
51+
if (isset($_SESSION[$tokenSessionKey])) {
52+
$client->setAccessToken($_SESSION[$tokenSessionKey]);
4153
}
4254

4355
// Check to ensure that the access token was successfully acquired.
4456
if ($client->getAccessToken()) {
57+
$htmlBody = '';
4558
try {
4659
// This code subscribes the authenticated user to the specified channel.
4760

@@ -78,7 +91,15 @@
7891
htmlspecialchars($e->getMessage()));
7992
}
8093

81-
$_SESSION['token'] = $client->getAccessToken();
94+
$_SESSION[$tokenSessionKey] = $client->getAccessToken();
95+
} elseif ($OAUTH2_CLIENT_ID == 'REPLACE_ME') {
96+
$htmlBody = <<<END
97+
<h3>Client Credentials Required</h3>
98+
<p>
99+
You need to set <code>\$OAUTH2_CLIENT_ID</code> and
100+
<code>\$OAUTH2_CLIENT_ID</code> before proceeding.
101+
<p>
102+
END;
82103
} else {
83104
// If the user has not authorized the application, start the OAuth 2.0 flow.
84105
$state = mt_rand();

php/captions.php

+37-20
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,23 @@
1212
* @author Ibrahim Ulukaya
1313
*/
1414

15+
/**
16+
* Library Requirements
17+
*
18+
* 1. Install composer (https://getcomposer.org)
19+
* 2. On the command line, change to this directory (api-samples/php)
20+
* 3. Require the google/apiclient library
21+
* $ composer require google/apiclient:~2.0
22+
*/
23+
if (!file_exists(__DIR__ . '/vendor/autoload.php')) {
24+
throw new \Exception('please run "composer require google/apiclient:~2.0" in "' . __DIR__ .'"');
25+
}
26+
27+
require_once __DIR__ . '/vendor/autoload.php';
28+
session_start();
29+
1530
$htmlBody = <<<END
16-
<form method="GET">
31+
<form method="POST" enctype="multipart/form-data">
1732
<div>
1833
Action:
1934
<select id="action" name="action">
@@ -50,12 +65,6 @@
5065
</form>
5166
END;
5267

53-
// Call set_include_path() as needed to point to your client library.
54-
require_once 'Google/Client.php';
55-
require_once 'Google/Service/YouTube.php';
56-
session_start();
57-
58-
5968
/*
6069
* You can acquire an OAuth 2.0 client ID and client secret from the
6170
* {{ Google Cloud Console }} <{{ https://cloud.google.com/console }}>
@@ -66,13 +75,6 @@
6675
$OAUTH2_CLIENT_ID = 'REPLACE_ME';
6776
$OAUTH2_CLIENT_SECRET = 'REPLACE_ME';
6877

69-
$action = $_GET['action'];
70-
$videoId = $_GET['videoId'];
71-
$captionFile = $_GET['captionFile'];
72-
$captionName = $_GET['captionName'];
73-
$captionLanguage = $_GET['captionLanguage'];
74-
$captionId = $_GET['captionId'];
75-
7678
$client = new Google_Client();
7779
$client->setClientId($OAUTH2_CLIENT_ID);
7880
$client->setClientSecret($OAUTH2_CLIENT_SECRET);
@@ -89,27 +91,34 @@
8991
// Define an object that will be used to make all API requests.
9092
$youtube = new Google_Service_YouTube($client);
9193

94+
// Check if an auth token exists for the required scopes
95+
$tokenSessionKey = 'token-' . $client->prepareScopes();
9296
if (isset($_GET['code'])) {
9397
if (strval($_SESSION['state']) !== strval($_GET['state'])) {
9498
die('The session state did not match.');
9599
}
96100

97101
$client->authenticate($_GET['code']);
98-
$_SESSION['token'] = $client->getAccessToken();
102+
$_SESSION[$tokenSessionKey] = $client->getAccessToken();
99103
header('Location: ' . $redirect);
100104
}
101105

102-
if (isset($_SESSION['token'])) {
103-
$client->setAccessToken($_SESSION['token']);
106+
if (isset($_SESSION[$tokenSessionKey])) {
107+
$client->setAccessToken($_SESSION[$tokenSessionKey]);
104108
}
105109

106110
// Check to ensure that the access token was successfully acquired.
107111
if ($client->getAccessToken()) {
108112
// This code executes if the user enters an action in the form
109113
// and submits the form. Otherwise, the page displays the form above.
110-
if ($_GET['action']) {
114+
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
115+
$videoId = isset($_POST['videoId']) ? $_POST['videoId'] : null;
116+
$captionFile = isset($_FILES['captionFile']) ? $_FILES['captionFile']['tmp_name'] : null;
117+
$captionName = isset($_POST['captionName']) ? $_POST['captionName'] : null;
118+
$captionLanguage = isset($_POST['captionLanguage']) ? $_POST['captionLanguage'] : null;
119+
$captionId = isset($_POST['captionId']) ? $_POST['captionId'] : null;
111120
try {
112-
switch ($action) {
121+
switch ($_POST['action']) {
113122
case 'upload':
114123
uploadCaption($youtube, $client, $videoId, $captionFile,
115124
$captionName, $captionLanguage, $htmlBody);
@@ -150,7 +159,15 @@
150159
htmlspecialchars($e->getMessage()));
151160
}
152161
}
153-
$_SESSION['token'] = $client->getAccessToken();
162+
$_SESSION[$tokenSessionKey] = $client->getAccessToken();
163+
} elseif ($OAUTH2_CLIENT_ID == 'REPLACE_ME') {
164+
$htmlBody = <<<END
165+
<h3>Client Credentials Required</h3>
166+
<p>
167+
You need to set <code>\$OAUTH2_CLIENT_ID</code> and
168+
<code>\$OAUTH2_CLIENT_ID</code> before proceeding.
169+
<p>
170+
END;
154171
} else {
155172
// If the user hasn't authorized the app, initiate the OAuth flow
156173
$state = mt_rand();

php/channel_localizations.php

+37-19
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,21 @@
1313
* @author Ibrahim Ulukaya
1414
*/
1515

16+
/**
17+
* Library Requirements
18+
*
19+
* 1. Install composer (https://getcomposer.org)
20+
* 2. On the command line, change to this directory (api-samples/php)
21+
* 3. Require the google/apiclient library
22+
* $ composer require google/apiclient:~2.0
23+
*/
24+
if (!file_exists(__DIR__ . '/vendor/autoload.php')) {
25+
throw new \Exception('please run "composer require google/apiclient:~2.0" in "' . __DIR__ .'"');
26+
}
27+
28+
require_once __DIR__ . '/vendor/autoload.php';
29+
session_start();
30+
1631
$htmlBody = <<<END
1732
<form method="GET">
1833
<div>
@@ -44,12 +59,6 @@
4459
</form>
4560
END;
4661

47-
// Call set_include_path() as needed to point to your client library.
48-
require_once 'Google/Client.php';
49-
require_once 'Google/Service/YouTube.php';
50-
session_start();
51-
52-
5362
/*
5463
* You can acquire an OAuth 2.0 client ID and client secret from the
5564
* {{ Google Cloud Console }} <{{ https://cloud.google.com/console }}>
@@ -60,13 +69,6 @@
6069
$OAUTH2_CLIENT_ID = 'REPLACE_ME';
6170
$OAUTH2_CLIENT_SECRET = 'REPLACE_ME';
6271

63-
$action = $_GET['action'];
64-
$resource = $_GET['resource'];
65-
$channelId = $_GET['channelId'];
66-
$language = $_GET['language'];
67-
$defaultLanguage = $_GET['defaultLanguage'];
68-
$description = $_GET['description'];
69-
7072
$client = new Google_Client();
7173
$client->setClientId($OAUTH2_CLIENT_ID);
7274
$client->setClientSecret($OAUTH2_CLIENT_SECRET);
@@ -83,27 +85,35 @@
8385
// Define an object that will be used to make all API requests.
8486
$youtube = new Google_Service_YouTube($client);
8587

88+
// Check if an auth token exists for the required scopes
89+
$tokenSessionKey = 'token-' . $client->prepareScopes();
8690
if (isset($_GET['code'])) {
8791
if (strval($_SESSION['state']) !== strval($_GET['state'])) {
8892
die('The session state did not match.');
8993
}
9094

9195
$client->authenticate($_GET['code']);
92-
$_SESSION['token'] = $client->getAccessToken();
96+
$_SESSION[$tokenSessionKey] = $client->getAccessToken();
9397
header('Location: ' . $redirect);
9498
}
9599

96-
if (isset($_SESSION['token'])) {
97-
$client->setAccessToken($_SESSION['token']);
100+
if (isset($_SESSION[$tokenSessionKey])) {
101+
$client->setAccessToken($_SESSION[$tokenSessionKey]);
98102
}
99103

100104
// Check to ensure that the access token was successfully acquired.
101105
if ($client->getAccessToken()) {
102106
// This code executes if the user enters an action in the form
103107
// and submits the form. Otherwise, the page displays the form above.
104-
if ($_GET['action']) {
108+
if (isset($_GET['action'])) {
109+
$htmlBody = '';
110+
$resource = $_GET['resource'];
111+
$channelId = $_GET['channelId'];
112+
$language = $_GET['language'];
113+
$defaultLanguage = $_GET['defaultLanguage'];
114+
$description = $_GET['description'];
105115
try {
106-
switch ($action) {
116+
switch ($_GET['action']) {
107117
case 'set':
108118
setChannelLocalization($youtube, $channelId, $defaultLanguage,
109119
$language, $description, $htmlBody);
@@ -123,7 +133,15 @@
123133
htmlspecialchars($e->getMessage()));
124134
}
125135
}
126-
$_SESSION['token'] = $client->getAccessToken();
136+
$_SESSION[$tokenSessionKey] = $client->getAccessToken();
137+
} elseif ($OAUTH2_CLIENT_ID == 'REPLACE_ME') {
138+
$htmlBody = <<<END
139+
<h3>Client Credentials Required</h3>
140+
<p>
141+
You need to set <code>\$OAUTH2_CLIENT_ID</code> and
142+
<code>\$OAUTH2_CLIENT_ID</code> before proceeding.
143+
<p>
144+
END;
127145
} else {
128146
// If the user hasn't authorized the app, initiate the OAuth flow
129147
$state = mt_rand();

0 commit comments

Comments
 (0)