2016-04-03 21 views
0

Bu yeni piyasaya sürülen yeni YouTube API'sı ile ilgili bir soru olduğundan, yinelenen bir durum değildir.YouTube Canlı Akışı Getirme Sohbet iletileri

Sohbet mesajlarını YouTube API 3'ten almaya çalışıyorum. Yapabileceğim her şeyi denedim ve takılıyorum.

Onları alabilmek için liveChatId öğesine ihtiyacım var ancak bu değeri nasıl alacağımı bilmiyorum. API, liveBroadcast snippet.liveChatId dosyasından aldığınızı, ancak bu yazıya eklediğim kod ile liveChatId alanında hiçbir değer olmadığını söylüyor. Sadece boş. LiveChatId'i nasıl düzgün şekilde alabilirim?

<?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> 

Neredeyim?

+0

Bunun neden bağlantılı sorunun bir kopyası olmadığını anlamıyorum. Bağlantılı dupe hedefi * yeni API'yi kullanır. – JAL

+0

Bağlantılı soru, kimliğin nasıl alınacağı ve nasıl kullanılacağı ile ilgilidir. Gerçekten bu şekilde elde etmeye çalışıyorum ama örnek kodumla liveChatId sonucunu geri alamıyorum. LiveChatId alanı boş. Bu yüzden yardıma ihtiyacım var! : D –

+0

'liveChatId''i kendi etkinliğinizden almaya mı çalışıyorsunuz? Başka bir kullanıcı değil mi? – JAL

cevap

0

Yorumunuz sorunu gidermiş. YouTube şu anda "Hemen Aktar" ile canlı çağrıları karıştırmayı ve API aracılığıyla Etkinlikler oluşturmayı desteklemiyor. Bir ya da diğerini kullanarak, her ikisini birden değiştirmeyen canlı etkinlikleri tamamlayın.

+0

Teşekkürler! Ama buraya bakın ... Bu: '$ 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; tr-US; rv: 1.8.1.13) Gecko/20080311 Firefox/2.0.0.13'); $ htmlBody = curl_exec ($ ch); curl_close ($ ch); ' Gives:" Oturum açmış olsam bile, belirtilen sohbet için iletileri almak için gerekli izinlere sahip değilsiniz. " Neden olduğuna dair bir fikrin var mı? –

İlgili konular