Skip to content

Commit 8213bb5

Browse files
"Added sample: php/list_streams.php"
1 parent fc5c6db commit 8213bb5

1 file changed

Lines changed: 90 additions & 0 deletions

File tree

php/list_streams.php

Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
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 the streams owned by the user who
47+
// authorized the request.
48+
$streamsResponse = $youtube->liveStreams->listLiveStreams('id,snippet', array(
49+
'mine' => 'true',
50+
));
51+
52+
$htmlBody .= "<h3>Live Streams</h3><ul>";
53+
foreach ($streamsResponse['items'] as $streamItem) {
54+
$htmlBody .= sprintf('<li>%s (%s)</li>', $streamItem['snippet']['title'],
55+
$streamItem['id']);
56+
}
57+
$htmlBody .= '</ul>';
58+
59+
} catch (Google_ServiceException $e) {
60+
$htmlBody .= sprintf('<p>A service error occurred: <code>%s</code></p>',
61+
htmlspecialchars($e->getMessage()));
62+
} catch (Google_Exception $e) {
63+
$htmlBody .= sprintf('<p>An client error occurred: <code>%s</code></p>',
64+
htmlspecialchars($e->getMessage()));
65+
}
66+
67+
$_SESSION['token'] = $client->getAccessToken();
68+
} else {
69+
// If the user hasn't authorized the app, initiate the OAuth flow
70+
$state = mt_rand();
71+
$client->setState($state);
72+
$_SESSION['state'] = $state;
73+
74+
$authUrl = $client->createAuthUrl();
75+
$htmlBody = <<<END
76+
<h3>Authorization Required</h3>
77+
<p>You need to <a href="$authUrl">authorize access</a> before proceeding.<p>
78+
END;
79+
}
80+
?>
81+
82+
<!doctype html>
83+
<html>
84+
<head>
85+
<title>My Live Streams</title>
86+
</head>
87+
<body>
88+
<?=$htmlBody?>
89+
</body>
90+
</html>

0 commit comments

Comments
 (0)