Advertisement
Guest User

Broadcast modifying the category

a guest
Oct 16th, 2015
244
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 6.83 KB | None | 0 0
  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 Developers Console <https://console.developers.google.com/>
  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.         // Create an object for the liveBroadcast resource's snippet. Specify values
  47.         // for the snippet's title, scheduled start time, and scheduled end time.
  48.         $broadcastSnippet = new Google_Service_YouTube_LiveBroadcastSnippet();
  49.         $broadcastSnippet->setTitle('New Broadcast');
  50.         $broadcastSnippet->setScheduledStartTime('2034-01-30T00:00:00.000Z');
  51.         $broadcastSnippet->setScheduledEndTime('2034-01-31T00:00:00.000Z');
  52.    
  53.         // Create an object for the liveBroadcast resource's status, and set the
  54.         // broadcast's status to "private".
  55.         $status = new Google_Service_YouTube_LiveBroadcastStatus();
  56.         $status->setPrivacyStatus('private');
  57.    
  58.         // Create the API request that inserts the liveBroadcast resource.
  59.         $broadcastInsert = new Google_Service_YouTube_LiveBroadcast();
  60.         $broadcastInsert->setSnippet($broadcastSnippet);
  61.         $broadcastInsert->setStatus($status);
  62.         $broadcastInsert->setKind('youtube#liveBroadcast');
  63.    
  64.         // Execute the request and return an object that contains information
  65.         // about the new broadcast.
  66.         $broadcastsResponse = $youtube->liveBroadcasts->insert('snippet,status',
  67.             $broadcastInsert, array());
  68.    
  69.         // Create an object for the liveStream resource's snippet. Specify a value
  70.         // for the snippet's title.
  71.         $streamSnippet = new Google_Service_YouTube_LiveStreamSnippet();
  72.         $streamSnippet->setTitle('New Stream');
  73.    
  74.         // Create an object for content distribution network details for the live
  75.         // stream and specify the stream's format and ingestion type.
  76.         $cdn = new Google_Service_YouTube_CdnSettings();
  77.         $cdn->setFormat("1080p");
  78.         $cdn->setIngestionType('rtmp');
  79.    
  80.         // Create the API request that inserts the liveStream resource.
  81.         $streamInsert = new Google_Service_YouTube_LiveStream();
  82.         $streamInsert->setSnippet($streamSnippet);
  83.         $streamInsert->setCdn($cdn);
  84.         $streamInsert->setKind('youtube#liveStream');
  85.    
  86.         // Execute the request and return an object that contains information
  87.         // about the new stream.
  88.         $streamsResponse = $youtube->liveStreams->insert('snippet,cdn',
  89.             $streamInsert, array());
  90.    
  91.         // Bind the broadcast to the live stream.
  92.         $bindBroadcastResponse = $youtube->liveBroadcasts->bind(
  93.             $broadcastsResponse['id'],'id,contentDetails',
  94.             array(
  95.                 'streamId' => $streamsResponse['id'],
  96.             ));
  97.        
  98.         //////////////////////////////////////////////////////////
  99.         //////////////////////// NEW AREA ////////////////////////
  100.         //////////////////////////////////////////////////////////
  101.        
  102.         // Creating the object to set the category
  103.         $videoSnippet = new Google_Service_YouTube_VideoSnippet();
  104.         $videoSnippet->setCategoryId("25");
  105.         //            $snippet->setCategoryId($category);
  106.        
  107.         $videoInsert = new Google_Service_YouTube_Video();
  108.         $videoInsert->setSnippet($videoSnippet);
  109.        
  110.         $insertRequest = $youtube->videos->insert("snippet", $videoInsert);
  111.        
  112.         //////////////////////////////////////////////////////////
  113.         //////////////////////////////////////////////////////////
  114.         //////////////////////////////////////////////////////////
  115.    
  116.         $htmlBody .= "<h3>Added Broadcast</h3><ul>";
  117.         $htmlBody .= sprintf('<li>%s published at %s (%s)</li>',
  118.             $broadcastsResponse['snippet']['title'],
  119.             $broadcastsResponse['snippet']['publishedAt'],
  120.             $broadcastsResponse['id']);
  121.         $htmlBody .= '</ul>';
  122.    
  123.         $htmlBody .= "<h3>Added Stream</h3><ul>";
  124.         $htmlBody .= sprintf('<li>%s (%s)</li>',
  125.             $streamsResponse['snippet']['title'],
  126.             $streamsResponse['id']);
  127.         $htmlBody .= '</ul>';
  128.    
  129.         $htmlBody .= "<h3>Bound Broadcast</h3><ul>";
  130.         $htmlBody .= sprintf('<li>Broadcast (%s) was bound to stream (%s).</li>',
  131.             $bindBroadcastResponse['id'],
  132.             $bindBroadcastResponse['contentDetails']['boundStreamId']);
  133.         $htmlBody .= '</ul>';
  134.    
  135.       } catch (Google_Service_Exception $e) {
  136.         $htmlBody .= sprintf('<p>A service error occurred: <code>%s</code></p>',
  137.             htmlspecialchars($e->getMessage()));
  138.       } catch (Google_Exception $e) {
  139.         $htmlBody .= sprintf('<p>An client error occurred: <code>%s</code></p>',
  140.             htmlspecialchars($e->getMessage()));
  141.       }
  142.    
  143.       $_SESSION['token'] = $client->getAccessToken();
  144.     } else {
  145.       // If the user hasn't authorized the app, initiate the OAuth flow
  146.       $state = mt_rand();
  147.       $client->setState($state);
  148.       $_SESSION['state'] = $state;
  149.    
  150.       $authUrl = $client->createAuthUrl();
  151.       $htmlBody = <<<END
  152.       <h3>Authorization Required</h3>
  153.       <p>You need to <a href="$authUrl">authorize access</a> before proceeding.<p>
  154.     END;
  155.     }
  156.     ?>
  157.    
  158.     <!doctype html>
  159.     <html>
  160.     <head>
  161.     <title>Bound Live Broadcast</title>
  162.     </head>
  163.     <body>
  164.       <?=$htmlBody?>
  165.     </body>
  166.     </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement