Php 获取YouTube实时流聊天信息

Php 获取YouTube实时流聊天信息,php,youtube-data-api,youtube-livestreaming-api,Php,Youtube Data Api,Youtube Livestreaming Api,因为这是一个关于刚刚发布的新YouTube API的问题,所以它不是重复的 我正在尝试从YouTube API 3获取聊天信息。我已经尝试了我能做的一切,我被卡住了 我需要liveChatId来获取它们,但我不知道如何检索该值。API说您可以从liveBroadcast snippet.liveChatId获得它,但是我在本文中附加的代码在liveChatId字段中没有任何值。它只是空的。如何正确检索liveChatId <?php session_start(); // Call se

因为这是一个关于刚刚发布的新YouTube API的问题,所以它不是重复的

我正在尝试从YouTube API 3获取聊天信息。我已经尝试了我能做的一切,我被卡住了

我需要liveChatId来获取它们,但我不知道如何检索该值。API说您可以从liveBroadcast snippet.liveChatId获得它,但是我在本文中附加的代码在liveChatId字段中没有任何值。它只是空的。如何正确检索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>

我在哪里迷路了?

你的评论暴露了这个问题。YouTube目前不支持将实时调用与“立即流”混合,并通过API创建事件。使用一个流或另一个流完成实时事件,而不是同时交换两个流。

我不明白为什么这不是链接问题的重复。链接的复制目标使用新的API。链接的问题是如何获取ID并使用它。我真的试图通过这种方式获得它,但我无法通过示例代码从结果中获取liveChatId。liveChatId字段为空。这就是我需要帮助的原因!:您正在尝试从自己的事件中获取
liveChatId
,对吗?不是其他用户的?是的,这正是我想要做的。我的事件被列出,但$broadcastItem['snippet']['liveChatId']始终为空!好的,我是维尔德。我现在可以拿到身份证了。。。但liveChatId似乎只有在我使用API创建广播时才生成(这很有效)。。。不是通过我的用户通过www.youtube.com?你认为这是为什么?谢谢!但是看这里。。。此:
$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);卷曲关闭($ch)给出:“您没有检索指定聊天信息所需的权限。”,即使我已正确登录。你知道为什么吗?