Skip to content

Commit 41e6964

Browse files
"Added sample: php/list_broadcasts.php"
1 parent 3e36eaa commit 41e6964

1 file changed

Lines changed: 92 additions & 0 deletions

File tree

php/list_broadcasts.php

Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
<?php
2+
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';
6+
session_start();
7+
8+
/*
9+
* You can acquire an OAuth 2.0 client ID and client secret from the
10+
* {{ Google Cloud Console }} <{{ https://cloud.google.com/console }}>
11+
* For more information about using OAuth 2.0 to access Google APIs, please see:
12+
* <https://developers.google.com/youtube/v3/guides/authentication>
13+
* Please ensure that you have enabled the YouTube Data API for your project.
14+
*/
15+
$OAUTH2_CLIENT_ID = 'REPLACE_ME';
16+
$OAUTH2_CLIENT_SECRET = 'REPLACE_ME';
17+
18+
$client = new Google_Client();
19+
$client->setClientId($OAUTH2_CLIENT_ID);
20+
$client->setClientSecret($OAUTH2_CLIENT_SECRET);
21+
$client->setScopes('https://www.googleapis.com/auth/youtube');
22+
$redirect = filter_var('http://' . $_SERVER['HTTP_HOST'] . $_SERVER['PHP_SELF'],
23+
FILTER_SANITIZE_URL);
24+
$client->setRedirectUri($redirect);
25+
26+
// Define an object that will be used to make all API requests.
27+
$youtube = new Google_Service_YouTube($client);
28+
29+
if (isset($_GET['code'])) {
30+
if (strval($_SESSION['state']) !== strval($_GET['state'])) {
31+
die('The session state did not match.');
32+
}
33+
34+
$client->authenticate($_GET['code']);
35+
$_SESSION['token'] = $client->getAccessToken();
36+
header('Location: ' . $redirect);
37+
}
38+
39+
if (isset($_SESSION['token'])) {
40+
$client->setAccessToken($_SESSION['token']);
41+
}
42+
43+
// Check to ensure that the access token was successfully acquired.
44+
if ($client->getAccessToken()) {
45+
try {
46+
// Execute an API request that lists broadcasts owned by the user who
47+
// authorized the request.
48+
$broadcastsResponse = $youtube->liveBroadcasts->listLiveBroadcasts(
49+
'id,snippet',
50+
array(
51+
'mine' => 'true',
52+
));
53+
54+
$htmlBody .= "<h3>Live Broadcasts</h3><ul>";
55+
foreach ($broadcastsResponse['items'] as $broadcastItem) {
56+
$htmlBody .= sprintf('<li>%s (%s)</li>', $broadcastItem['snippet']['title'],
57+
$broadcastItem['id']);
58+
}
59+
$htmlBody .= '</ul>';
60+
61+
} catch (Google_ServiceException $e) {
62+
$htmlBody .= sprintf('<p>A service error occurred: <code>%s</code></p>',
63+
htmlspecialchars($e->getMessage()));
64+
} catch (Google_Exception $e) {
65+
$htmlBody .= sprintf('<p>An client error occurred: <code>%s</code></p>',
66+
htmlspecialchars($e->getMessage()));
67+
}
68+
69+
$_SESSION['token'] = $client->getAccessToken();
70+
} else {
71+
// If the user hasn't authorized the app, initiate the OAuth flow
72+
$state = mt_rand();
73+
$client->setState($state);
74+
$_SESSION['state'] = $state;
75+
76+
$authUrl = $client->createAuthUrl();
77+
$htmlBody = <<<END
78+
<h3>Authorization Required</h3>
79+
<p>You need to <a href="$authUrl">authorize access</a> before proceeding.<p>
80+
END;
81+
}
82+
?>
83+
84+
<!doctype html>
85+
<html>
86+
<head>
87+
<title>My Live Broadcasts</title>
88+
</head>
89+
<body>
90+
<?=$htmlBody?>
91+
</body>
92+
</html>

0 commit comments

Comments
 (0)