0
我使用的Youtube API從我在YouTube上直播streaming.That通過以下code.But我的要求做成功創建應用程序的事件:啓用的Youtube API直播
1)我要開始直播從我的應用程序界面使用youtube流 API。是否有可能?
2)當我使用youtube API從應用程序創建事件時,它會自動轉到Custom (more encoding options)
type.I需要在Quick (using Google+ Hangouts On Air)
type中創建實時流式事件。是否有可能?
我的代碼:
<?php
// Call set_include_path() as needed to point to your client library.
require_once 'Google/autoload.php';
require_once 'Google/Client.php';
require_once 'Google/Service/YouTube.php';
session_start();
//'2015-08-28T00:00:00.000Z'
//'2015-08-29T00:00:00.000Z'
/*
* You can acquire an OAuth 2.0 client ID and client secret from the
* Google Developers Console <https://console.developers.google.com/>
* 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.
*/
?>
<script type="text/javascript">
var datefield=document.createElement("input")
datefield.setAttribute("type", "date")
if (datefield.type!="date"){ //if browser doesn't support input type="date", load files for jQuery UI Date Picker
document.write('<link href="http://ajax.googleapis.com/ajax/libs/jqueryui/1.8/themes/base/jquery-ui.css" rel="stylesheet" type="text/css" />\n')
document.write('<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.4/jquery.min.js"><\/script>\n')
document.write('<script src="http://ajax.googleapis.com/ajax/libs/jqueryui/1.8/jquery-ui.min.js"><\/script>\n')
}
</script>
<script>
if (datefield.type!="date"){ //if browser doesn't support input type="date", initialize date picker widget:
jQuery(function($){ //on document.ready
$('.dateStart').datepicker();
$('.dateEnd').datepicker();
})
}
</script>
<?php
$OAUTH2_CLIENT_ID = 'dsfsfsdfofgm9e4uvg.apps.googleusercontent.com';
$OAUTH2_CLIENT_SECRET = 'dsfsdfdsfPhf55tcCDLu';
$htmlBody="";
$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()) {
if(isset($_POST['title']) && isset($_POST['timeStart']) && isset($_POST['timeEnd']) && isset($_POST['Status']) && isset($_POST['dateStart']) && isset($_POST['dateEnd']))
{
try {
// Create an object for the liveBroadcast resource's snippet. Specify values
// for the snippet's title, scheduled start time, and scheduled end time.
$startTime = $_POST['dateStart']."T".$_POST['timeStart'].":00+0530";
$endTime = $_POST['dateEnd']."T".$_POST['timeEnd'].":00+0530";
//$startDatetime = new DateTime($startTime);
//$endDatetime = new DateTime($endTime);
//$startDatetime = $startDatetime->format(DateTime::ISO8601);
//$endDatetime = $endDatetime->format(DateTime::ISO8601);
$broadcastSnippet = new Google_Service_YouTube_LiveBroadcastSnippet();
$broadcastSnippet->setTitle($_POST['title']);
$broadcastSnippet->setScheduledStartTime($startTime);
$broadcastSnippet->setScheduledEndTime($endTime);
// Create an object for the liveBroadcast resource's status, and set the
// broadcast's status to "private".
$status = new Google_Service_YouTube_LiveBroadcastStatus();
$status->setPrivacyStatus($_POST['Status']);
// Create the API request that inserts the liveBroadcast resource.
$broadcastInsert = new Google_Service_YouTube_LiveBroadcast();
$broadcastInsert->setSnippet($broadcastSnippet);
$broadcastInsert->setStatus($status);
$broadcastInsert->setKind('youtube#liveBroadcast');
// Execute the request and return an object that contains information
// about the new broadcast.
$broadcastsResponse = $youtube->liveBroadcasts->insert('snippet,status',
$broadcastInsert, array());
// Create an object for the liveStream resource's snippet. Specify a value
// for the snippet's title.
$streamSnippet = new Google_Service_YouTube_LiveStreamSnippet();
$streamSnippet->setTitle('New Stream');
// Create an object for content distribution network details for the live
// stream and specify the stream's format and ingestion type.
$cdn = new Google_Service_YouTube_CdnSettings();
$cdn->setFormat("1080p");
$cdn->setIngestionType('rtmp');
// Create the API request that inserts the liveStream resource.
$streamInsert = new Google_Service_YouTube_LiveStream();
$streamInsert->setSnippet($streamSnippet);
$streamInsert->setCdn($cdn);
$streamInsert->setKind('youtube#liveStream');
// Execute the request and return an object that contains information
// about the new stream.
$streamsResponse = $youtube->liveStreams->insert('snippet,cdn',
$streamInsert, array());
// Bind the broadcast to the live stream.
$bindBroadcastResponse = $youtube->liveBroadcasts->bind(
$broadcastsResponse['id'],'id,contentDetails',
array(
'streamId' => $streamsResponse['id'],
));
$htmlBody .= "<h3>Your Broadcast success fully scheduled.</h3><ul>";
$htmlBody .= sprintf('<li>%s published at %s (%s)</li>',
$broadcastsResponse['snippet']['title'],
$broadcastsResponse['snippet']['publishedAt'],
$broadcastsResponse['id']);
$htmlBody .= '</ul>';
$htmlBody .= "<h3>Added Stream</h3><ul>";
$htmlBody .= sprintf('<li>%s (%s)</li>',
$streamsResponse['snippet']['title'],
$streamsResponse['id']);
$htmlBody .= '</ul>';
$htmlBody .= "<h3>Bound Broadcast</h3><ul>";
$htmlBody .= sprintf('<li>Broadcast (%s) was bound to stream (%s).</li>',
$bindBroadcastResponse['id'],
$bindBroadcastResponse['contentDetails']['boundStreamId']);
$htmlBody .= '</ul>';
//$htmlBody .="<h3>Live Broadcast</h3><div>";
//$htmlBody .= sprintf("<iframe id='ytplayer' type='text/html' width='640' height='390' src='http://www.youtube.com/embed/%s?autoplay=1' frameborder='0'></iframe>",
// $broadcastsResponse['id']);
$htmlBody .= '</div>';
} catch (Google_Service_Exception $e) {
$htmlBody .= sprintf('<p>A service error occurred: <code>%s</code></p>',
htmlspecialchars($e->getMessage()));
// echo $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(isset($_GET['propid']))
{
$projectname = $_GET['name'];
$htmlBody = <<<END
<form action="" method="post">
<label>Event Title: $projectname</label>
<input type="hidden" name="title" value="$projectname"/><br/><br/>
<label>Start Time:</label>
<input type="date" name="dateStart"/>
<select name="timeStart">
<option value="00:00">12:00 AM</option>
<option value="00:30">12:30 AM</option>
<option value="01:00">01 AM</option>
<option value="01:30">01:30 AM</option>
<option value="02:00">02:00 AM</option>
<option value="02:30">02:30 AM</option>
<option value="03:00">03:00 AM</option>
<option value="03:30">03:30 AM</option>
<option value="04:00">04:00 AM</option>
<option value="04:30">04:30 AM</option>
<option value="05:00">05:00 AM</option>
<option value="05:30">05:30 AM</option>
<option value="06:00">06:00 AM</option>
<option value="06:30">06:30 AM</option>
<option value="07:00">07:00 AM</option>
<option value="07:30">07:30 AM</option>
<option value="08:00">08:00 AM</option>
<option value="08:30">08:30 AM</option>
<option value="09:00">09:00 AM</option>
<option value="09:30">09:30 AM</option>
<option value="10:00">10:00 AM</option>
<option value="10:30">10:30 AM</option>
<option value="11:00">11:00 AM</option>
<option value="11:30">11:30 AM</option>
<option value="12:00">12:00 PM</option>
<option value="12:30">12:30 PM</option>
<option value="13:00">01:00 PM</option>
<option value="13:30">01:30 PM</option>
<option value="14:00">02:00 PM</option>
<option value="14:30">02:30 PM</option>
<option value="15:00">03:00 PM</option>
<option value="15:30">03:30 PM</option>
<option value="16:00">04:00 PM</option>
<option value="16:30">04:30 PM</option>
<option value="17:00">5:00 PM</option>
<option value="17:30">5:30 PM</option>
<option value="18:00">6:00 PM</option>
<option value="18:30">6:30 PM</option>
<option value="19:00">7:00 PM</option>
<option value="19:30">7:30 PM</option>
<option value="20:00">8:00 PM</option>
<option value="20:30">8:30 PM</option>
<option value="21:00">9:00 PM</option>
<option value="21:30">9:30 PM</option>
<option value="22:00">10:00 PM</option>
<option value="22:30">10:30 PM</option>
<option value="23:00">11:00 PM</option>
<option value="23:30">11:30 PM</option>
</select>
<br/><br/>
<label>End Time:</label>
<input type="date" name="dateEnd"/>
<select name="timeEnd">
<option value="00:00">12:00 AM</option>
<option value="00:30">12:30 AM</option>
<option value="01:00">01 AM</option>
<option value="01:30">01:30 AM</option>
<option value="02:00">02:00 AM</option>
<option value="02:30">02:30 AM</option>
<option value="03:00">03:00 AM</option>
<option value="03:30">03:30 AM</option>
<option value="04:00">04:00 AM</option>
<option value="04:30">04:30 AM</option>
<option value="05:00">05:00 AM</option>
<option value="05:30">05:30 AM</option>
<option value="06:00">06:00 AM</option>
<option value="06:30">06:30 AM</option>
<option value="07:00">07:00 AM</option>
<option value="07:30">07:30 AM</option>
<option value="08:00">08:00 AM</option>
<option value="08:30">08:30 AM</option>
<option value="09:00">09:00 AM</option>
<option value="09:30">09:30 AM</option>
<option value="10:00">10:00 AM</option>
<option value="10:30">10:30 AM</option>
<option value="11:00">11:00 AM</option>
<option value="11:30">11:30 AM</option>
<option value="12:00">12:00 PM</option>
<option value="12:30">12:30 PM</option>
<option value="13:00">01:00 PM</option>
<option value="13:30">01:30 PM</option>
<option value="14:00">02:00 PM</option>
<option value="14:30">02:30 PM</option>
<option value="15:00">03:00 PM</option>
<option value="15:30">03:30 PM</option>
<option value="16:00">04:00 PM</option>
<option value="16:30">04:30 PM</option>
<option value="17:00">5:00 PM</option>
<option value="17:30">5:30 PM</option>
<option value="18:00">6:00 PM</option>
<option value="18:30">6:30 PM</option>
<option value="19:00">7:00 PM</option>
<option value="19:30">7:30 PM</option>
<option value="20:00">8:00 PM</option>
<option value="20:30">8:30 PM</option>
<option value="21:00">9:00 PM</option>
<option value="21:30">9:30 PM</option>
<option value="22:00">10:00 PM</option>
<option value="22:30">10:30 PM</option>
<option value="23:00">11:00 PM</option>
<option value="23:30">11:30 PM</option>
</select>
<br/><br/>
<input type="hidden" name="Status" value="unlisted"/>
<!--<label>Stream Status:</label>
<select name="Status">
<option value="public">Public</option>
<option value="unlisted">Unlisted</option>
<option value="private">Private</option>
</select> <br/><br/>-->
<label>Participants:</label>
<input type="text" name="emails" placeholder="Comma separated emails"/><br/><br/>
<input type ="submit" value="Submit"/>
</form>
END;
} else {
$htmlBody = <<<END
<p>Close this window and click the schedule button for live streaming</p>
<p>You need to <a href="#" onclick="window.close();">Close Window</a> before proceeding.<p>
END;
}
}
} 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" onclick="window.open('$authUrl', 'newwindow', 'width=600, height=400'); return false;">authorize access</a> before proceeding.<p>
END;
}
?>
<!doctype html>
<html>
<head>
<title>Bound Live Broadcast</title>
</head>
<body>
<?=$htmlBody?>
</body>
</html>
您可以使用[插入](https://developers.google.com/youtube/v3/live/docs/liveStreams/insert)創建一個具有指定的開始時間的視頻直播,但我不知道你是否會考慮「開始直播」。我相信只有通過YouTube頻道進行直播時,您才能使用Google+環聊直播,而不是API。 –
我可以使用API創建活動。是否可以使用youtube API啓用Google +環聊選項? – Kichu
我認爲如果你還使用Google+環聊API,你也許可以做到這一點。請參閱[這裏](https://developers.google.com/+/hangouts/button)和[這裏](https://developers.google.com/+/hangouts/api/gapi.hangout.onair)。 –