0

Since this is a question about the new YouTube API that has just been released it is not a duplicate.

I am trying to fetch the chat messages from the YouTube API 3. I have tried everything I can and I am stuck.

I need the liveChatId to be able to fetch them but I don't know how to retrieve that value. The API says that you get it from liveBroadcast snippet.liveChatId, but with the code I've attached in this post there is no value in the liveChatId field. It is just empty. How do I properly retrieve the liveChatId?

<?php
session_start();

// 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';

$OAUTH2_CLIENT_ID = 'MY-CLIENT-ID';
$OAUTH2_CLIENT_SECRET = 'MY-CLIENT-SECRET';

$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);
$_SESSION['state'] = $_GET['state'];
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()) {
  try {
    // Execute an API request that lists broadcasts owned by the user who
    // authorized the request.
    $broadcastsResponse = $youtube->liveBroadcasts->listLiveBroadcasts(
        'id,snippet',
        array(
            'mine' => 'true',
        ));

var_dump($broadcastsResponse['items']);

    $htmlBody .= "<h3>Live Broadcasts</h3><ul>";
    foreach ($broadcastsResponse['items'] as $broadcastItem) {
      $htmlBody .= $broadcastItem['snippet']['title'] ."<br />Chat ID: ". $broadcastItem['liveChatId'] ."<br />ID: ". $broadcastItem['id'];
    }
    $htmlBody .= '</ul>';

  } catch (Google_Service_Exception $e) {
    $htmlBody .= sprintf('<p>A service error occurred: <code>%s</code></p>',
        htmlspecialchars($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 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">authorize access</a> before proceeding.<p>
END;
}
?>

<!doctype html>
<html>
<head>
<title>My Live Streams</title>
</head>
<body>
  <?=$htmlBody?>
</body>
</html>

Where am I lost?

JAL
  • 41,701
  • 23
  • 172
  • 300
  • I don't understand why this isn't a duplicate of the linked question. The linked dupe target *uses* the new API. – JAL Apr 04 '16 at 16:17
  • The linked question is about how to obtain the ID and use it. I really try to obtain it that way but I can not get the liveChatId back from the result with my example code. The liveChatId field is empty. That is why I need help! :D – Osgar Schölander Apr 04 '16 at 16:38
  • You are trying to fetch the `liveChatId` from your own event right? Not another user's? – JAL Apr 04 '16 at 16:39
  • Yepp, that is exactly what I'm trying to do. My events gets listed, but the $broadcastItem['snippet']['liveChatId'] is always empty! – Osgar Schölander Apr 04 '16 at 16:47
  • Okey, this is wierd. I can get the ID now... But the liveChatId only appears to be generated when I create a broadcast with the API (which works)... not through my user via www.youtube.com? Why is that do you think? – Osgar Schölander Apr 04 '16 at 16:50

1 Answers1

0

Your comment has exposed the problem. YouTube currently doesn't support mixing live calls with "Stream Now" and creating Events via the API. Complete live events using either one flow or the other, not interchanging both.

JAL
  • 41,701
  • 23
  • 172
  • 300
  • Thanks! But look here... This: `$url="https://www.googleapis.com/youtube/v3/liveChat/messages? liveChatId=MYID&part=snippet&key=MYAPIKEY"; $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, $url); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); curl_setopt($ch,CURLOPT_USERAGENT,'Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.13) Gecko/20080311 Firefox/2.0.0.13'); $htmlBody = curl_exec($ch); curl_close($ch);` Gives: "You do not have the necessary permissions to retrieve messages for the specified chat.", even though I'm logged in correctly. Any ideas on why? – Osgar Schölander Apr 22 '16 at 17:41