Advertisement
Guest User

Untitled

a guest
Sep 1st, 2015
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.48 KB | None | 0 0
  1. <?php
  2.  
  3. // Call set_include_path() as needed to point to your client library.
  4. require_once 'Google/autoload.php';
  5. require_once 'Google/Client.php';
  6. require_once 'Google/Service/YouTube.php';
  7. session_start();
  8.  
  9. /*
  10. * You can acquire an OAuth 2.0 client ID and client secret from the
  11. * Google Developers Console <https://console.developers.google.com/>
  12. * For more information about using OAuth 2.0 to access Google APIs, please see:
  13. * <https://developers.google.com/youtube/v3/guides/authentication>
  14. * Please ensure that you have enabled the YouTube Data API for your project.
  15. */
  16. $OAUTH2_CLIENT_ID = '$OAUTH2_CLIENT_ID';
  17. $OAUTH2_CLIENT_SECRET = '$OAUTH2_CLIENT_SECRET';
  18. $htmlBody="";
  19. $client = new Google_Client();
  20. $client->setClientId($OAUTH2_CLIENT_ID);
  21. $client->setClientSecret($OAUTH2_CLIENT_SECRET);
  22. $client->setScopes('https://www.googleapis.com/auth/youtube');
  23. $redirect = filter_var('http://' . $_SERVER['HTTP_HOST'] . $_SERVER['PHP_SELF'],
  24. FILTER_SANITIZE_URL);
  25. $client->setRedirectUri($redirect);
  26.  
  27. // Define an object that will be used to make all API requests.
  28. $youtube = new Google_Service_YouTube($client);
  29.  
  30. if (isset($_GET['code'])) {
  31. if (strval($_SESSION['state']) !== strval($_GET['state'])) {
  32. die('The session state did not match.');
  33. }
  34.  
  35. $client->authenticate($_GET['code']);
  36. $_SESSION['token'] = $client->getAccessToken();
  37. header('Location: ' . $redirect);
  38. }
  39.  
  40. if (isset($_SESSION['token'])) {
  41. $client->setAccessToken($_SESSION['token']);
  42. }
  43.  
  44. // Check to ensure that the access token was successfully acquired.
  45. if ($client->getAccessToken()) {
  46. if(isset($_POST['title']) && isset($_POST['timeStart']) && isset($_POST['timeEnd']) && isset($_POST['Status']))
  47. {
  48. try {
  49. // Create an object for the liveBroadcast resource's snippet. Specify values
  50. // for the snippet's title, scheduled start time, and scheduled end time.
  51. $broadcastSnippet = new Google_Service_YouTube_LiveBroadcastSnippet();
  52. $broadcastSnippet->setTitle($_POST['title']);
  53. $broadcastSnippet->setScheduledStartTime($_POST['timeStart']);
  54. $broadcastSnippet->setScheduledEndTime($_POST['timeEnd']);
  55.  
  56. // Create an object for the liveBroadcast resource's status, and set the
  57. // broadcast's status to "private".
  58. $status = new Google_Service_YouTube_LiveBroadcastStatus();
  59. $status->setPrivacyStatus($_POST['Status']);
  60.  
  61. // Create the API request that inserts the liveBroadcast resource.
  62. $broadcastInsert = new Google_Service_YouTube_LiveBroadcast();
  63. $broadcastInsert->setSnippet($broadcastSnippet);
  64. $broadcastInsert->setStatus($status);
  65. $broadcastInsert->setKind('youtube#liveBroadcast');
  66.  
  67. // Execute the request and return an object that contains information
  68. // about the new broadcast.
  69. $broadcastsResponse = $youtube->liveBroadcasts->insert('snippet,status',
  70. $broadcastInsert, array());
  71.  
  72. // Create an object for the liveStream resource's snippet. Specify a value
  73. // for the snippet's title.
  74. $streamSnippet = new Google_Service_YouTube_LiveStreamSnippet();
  75. $streamSnippet->setTitle('New Stream');
  76.  
  77. // Create an object for content distribution network details for the live
  78. // stream and specify the stream's format and ingestion type.
  79. $cdn = new Google_Service_YouTube_CdnSettings();
  80. $cdn->setFormat("1080p");
  81. $cdn->setIngestionType('rtmp');
  82.  
  83. // Create the API request that inserts the liveStream resource.
  84. $streamInsert = new Google_Service_YouTube_LiveStream();
  85. $streamInsert->setSnippet($streamSnippet);
  86. $streamInsert->setCdn($cdn);
  87. $streamInsert->setKind('youtube#liveStream');
  88.  
  89. // Execute the request and return an object that contains information
  90. // about the new stream.
  91. $streamsResponse = $youtube->liveStreams->insert('snippet,cdn',
  92. $streamInsert, array());
  93.  
  94. // Bind the broadcast to the live stream.
  95. $bindBroadcastResponse = $youtube->liveBroadcasts->bind(
  96. $broadcastsResponse['id'],'id,contentDetails',
  97. array(
  98. 'streamId' => $streamsResponse['id'],
  99. ));
  100.  
  101. $htmlBody .= "<h3>Added Broadcast</h3><ul>";
  102. $htmlBody .= sprintf('<li>%s published at %s (%s)</li>',
  103. $broadcastsResponse['snippet']['title'],
  104. $broadcastsResponse['snippet']['publishedAt'],
  105. $broadcastsResponse['id']);
  106. $htmlBody .= '</ul>';
  107. $htmlBody .= "<h3>Added Stream</h3><ul>";
  108. $htmlBody .= sprintf('<li>%s (%s)</li>',
  109. $streamsResponse['snippet']['title'],
  110. $streamsResponse['id']);
  111. $htmlBody .= '</ul>';
  112. $htmlBody .= "<h3>Bound Broadcast</h3><ul>";
  113. $htmlBody .= sprintf('<li>Broadcast (%s) was bound to stream (%s).</li>',
  114. $bindBroadcastResponse['id'],
  115. $bindBroadcastResponse['contentDetails']['boundStreamId']);
  116. $htmlBody .= '</ul>';
  117. //$htmlBody .="<h3>Live Broadcast</h3><div>";
  118.  
  119. //$htmlBody .= sprintf("<iframe id='ytplayer' type='text/html' width='640' height='390' src='http://www.youtube.com/embed/%s?autoplay=1' frameborder='0'></iframe>",
  120. // $broadcastsResponse['id']);
  121. $htmlBody .= '</div>';
  122.  
  123.  
  124. } catch (Google_Service_Exception $e) {
  125. $htmlBody .= sprintf('<p>A service error occurred: <code>%s</code></p>',
  126. htmlspecialchars($e->getMessage()));
  127. // echo $e->getMessage();
  128. } catch (Google_Exception $e) {
  129. $htmlBody .= sprintf('<p>An client error occurred: <code>%s</code></p>',
  130. htmlspecialchars($e->getMessage()));
  131. }
  132.  
  133. $_SESSION['token'] = $client->getAccessToken();
  134. } else {
  135. if(isset($_GET['propid']))
  136. {
  137. $htmlBody = <<<END
  138. <form action="" method="post">
  139. <input type="text" name="title" value="Event Title"/><br/>
  140. <input type="text" name="timeStart" value="Start Time"/><br/>
  141. <input type="text" name="timeEnd" value="End Time"/><br/>
  142. <input type="text" name="Status" value="Public,Unlisted,Private"/>
  143. <input type ="submit" value="Submit"/>
  144. </form>
  145. END;
  146. } else {
  147.  
  148. $htmlBody = <<<END
  149. <p>Close this window and click the schedule button for live streaming</p>
  150. <p>You need to <a href="#" onclick="window.close();">Close Window</a> before proceeding.<p>
  151. END;
  152.  
  153. }
  154.  
  155. }
  156. } else {
  157. // If the user hasn't authorized the app, initiate the OAuth flow
  158. $state = mt_rand();
  159. $client->setState($state);
  160. $_SESSION['state'] = $state;
  161.  
  162. $authUrl = $client->createAuthUrl();
  163. $htmlBody = <<<END
  164. <h3>Authorization Required</h3>
  165. <p>You need to <a href="$authUrl" onclick="window.open('$authUrl', 'newwindow', 'width=600, height=400'); return false;">authorize access</a> before proceeding.<p>
  166. END;
  167. }
  168. ?>
  169.  
  170. <!doctype html>
  171. <html>
  172. <head>
  173. <title>Bound Live Broadcast</title>
  174. </head>
  175. <body>
  176. <?=$htmlBody?>
  177. </body>
  178. </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement