Youtube API - 订阅推送通知

时间:2015-04-13 17:31:45

标签: xml push-notification youtube-api pubsubhubbub

我的最终目标是在YouTube用户上传视频时设置Webhook。经过一些研究,我得到this article

但是当我到达https://www.youtube.com/xml/feeds/videos.xml?channel_id=CHANNEL_ID部分时,在尝试订阅Google / SuperFeedr集线器时出现Restricted topic错误。我的回调网址也正常工作。

我想订阅的主题是:https://www.youtube.com/xml/feeds/videos.xml?channel_id=UC7T8roVtC_3afWKTOGtLlBA

通过浏览器访问时没有显示任何内容。

我做错了吗?我已经挣扎了几个小时了,任何帮助都表示赞赏。谢谢!

更新:我找到this,但这些Feed没有rel=”hub”属性,因此如果我想将其订阅到套点,则可能无用。

3 个答案:

答案 0 :(得分:4)

  1. 您需要发送订阅频道的请求
  2. 一个回调,它会更改订阅请求并获取有关更新的实际数据
  3. 订阅功能:

    subscribe.php 可能如下所示:

    <?php
    
    function subscribeYoutubeChannel($channel_id = null, $subscribe = true) {
        $subscribe_url = 'https://pubsubhubbub.appspot.com/subscribe';
        $topic_url = 'https://www.youtube.com/xml/feeds/videos.xml?channel_id={CHANNEL_ID}';
        $callback_url = 'http://' . $_SERVER['SERVER_NAME'] . str_replace(basename($_SERVER['REQUEST_URI']), '', $_SERVER['REQUEST_URI']) . 'youtube_subscribe_callback.php';
    
        $data = array(
            'hub.mode' => $subscribe ? 'subscribe' : 'unsubscribe',
            'hub.callback' => $callback_url,
            'hub.lease_seconds'=>60*60*24*365,
            'hub.topic'=> str_replace(array(
                '{CHANNEL_ID}'
            ), array(
                $channel_id
            ), $topic_url)
        );
    
        $opts = array('http' =>
            array(
                'method'  => 'POST',
                'header'  => 'Content-type: application/x-www-form-urlencoded',
                'content' => http_build_query($data)
            )
        );
    
        $context  = stream_context_create($opts);
    
        @file_get_contents($subscribe_url, false, $context);
    
        return preg_match('200', $http_response_header[0]) === 1;
    }
    

    发送请求后,pusub服务将调用youtube_subscribe_callback.php来验证订阅 它将使用GET方法,它希望得到一个答案,即&#34; hub_challenge&#34;。 之后,如果您将视频上传到测试频道,youtube_subscribe_callback.php将收到包含数据的POST请求。

    所以 youtube_subscribe_callback.php (在subscribeYoutubeChannel函数中定义)可能如下所示:

     <?php
        if (isset($_GET['hub_challenge'])) {
            echo $_REQUEST['hub_challenge'];
        } else {
    
            $video = parseYoutubeUpdate(file_get_contents('php://input'));
    
        }
    
        function parseYoutubeUpdate($data) {
            $xml = simplexml_load_string($data, 'SimpleXMLElement', LIBXML_NOCDATA);
            $video_id = substr((string)$xml->entry->id, 9);
            $channel_id = substr((string)$xml->entry->author->uri, 32);
            $published = (string)$xml->entry->published;
    
            return array(
                'video_id'=>$video_id,
                'channel_id'=>$channel_id,
                'published'=>$published
            );
        }
    

答案 1 :(得分:2)

我无法按ID订阅频道,但能够通过用户名进行订阅:

https://www.youtube.com/feeds/videos.xml?user=username

所以,你转到这个页面:

https://pubsubhubbub.appspot.com/subscribe

使用用户名和“订阅”模式插入您的回调网址,来自youtube的RSS Feed。

不要忘记回复你的回调网址,这样它就可以确认订阅,在PHP中只打印:

echo $_REQUEST["hub_challenge"];

更多详情herehere

答案 2 :(得分:1)

此过程通常分为两个步骤,首先进入subscribe页,输入回调服务器url,主题url(基本上是您要收听的ytb频道的供稿url,其他字段是可选的),发布服务器会通过向您的回调服务器发送GET请求来验证您的订阅,在执行过程中,它将看起来像这样:

http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
    challenge := r.URL.Query().Get("hub.challenge")
    if challenge != "" {
        fmt.Fprintf(w, challenge)
    }
})

然后,在每个新视频(或标题,旧视频的描述已更新)上,酒馆会向您的服务器提交一个POST请求,其中的xml内容类似于以下内容:

<?xml version='1.0' encoding='UTF-8'?>
<feed xmlns:yt="http://www.youtube.com/xml/schemas/2015" xmlns="http://www.w3.org/2005/Atom">
  <link rel="hub" href="https://pubsubhubbub.appspot.com" />
  <link rel="self" href="https://www.youtube.com/xml/feeds/videos.xml?channel_id=UCtEorrVfo4GQsN82HsrnKyk" />
  <title>YouTube video feed</title>
  <updated>2018-12-12T06:02:55.950497732+00:00</updated>
  <entry>
    <id>yt:video:_em_FFNUcvs</id>
    <yt:videoId>_em_FFNUcvs</yt:videoId>
    <yt:channelId>UCtEorrVfo4GQsN82HsrnKyk</yt:channelId>
    <title>December 12, 20</title>
    <link rel="alternate" href="https://www.youtube.com/watch?v=_em_FFNUcvs" />
    <author>
      <name>Ak Ram</name>
      <uri>https://www.youtube.com/channel/UCtEorrVfo4GQsN82HsrnKyk</uri>
    </author>
    <published>2018-12-12T05:57:07+00:00</published>
    <updated>2018-12-12T06:02:55.950497732+00:00</updated>
  </entry>
</feed>