Youtube API - 订阅推送通知

问题描述 投票:0回答:4

我的最终目标是每当 YouTube 用户上传视频时就设置一个 Webhook。经过一番研究后,我看到了这篇文章

但是当我到达

https://www.youtube.com/xml/feeds/videos.xml?channel_id=CHANNEL_ID
部分时,在尝试订阅Google/SuperFeedr中心时出现了
Restricted topic
错误。我的回调 URL 也正常工作了。

我要订阅的主题是这样的:

https://www.youtube.com/xml/feeds/videos.xml?channel_id=UC7T8roVtC_3afWKTOGtLlBA

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

我做错了什么吗?我已经挣扎了几个小时了,非常感谢任何帮助。谢谢!

更新:我找到了this,但这些提要没有

rel=”hub”
属性,所以如果我想将其订阅到集线器,可能就没用了。

xml push-notification youtube-api websub
4个回答
9
投票
  1. 您需要发送请求来订阅频道
  2. 一个回调,用于改变订阅请求并获取更新时的实际数据

订阅功能:

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 方法,并期望收到“hub_challenge”的答案。 之后,如果您将视频上传到测试频道 youtube_subscribe_callback.php 将收到带有数据的 POST 请求。

so 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
        );
    }

3
投票

我无法通过 ID 订阅频道,但可以通过用户名订阅:

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

因此,您转到此页面:

https://pubsubhubbub.appspot.com/subscribe

插入您的回调 URL、来自 YouTube 的 RSS 提要以及用户名和“订阅”模式。

不要忘记从回调 URL 中回复,这样它就可以确认订阅,在 PHP 中只需打印:

echo $_REQUEST["hub_challenge"];

更多详情这里这里


3
投票

这个过程通常是两个步骤,首先你进入subscribe页面,输入你的回调服务器url,主题url(基本上是你想收听的ytb频道的feed url,其他字段是可选的), pub 服务器将通过向您的回调服务器发出

GET
请求来验证您的订阅,在 go 中它可能如下所示:

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>

1
投票

我认为指南的网址是错误的

https://www.youtube.com/xml/feeds/videos.xml
不再工作了。但如果没有
/xml
那么就可以工作了。

有样品。

https://www.youtube.com/feeds/videos.xml?channel_id=UCJHiO2Dfao-qpYiA79swtAA https://www.youtube.com/xml/feeds/videos.xml?channel_id=UCJHiO2Dfao-qpYiA79swtAA

© www.soinside.com 2019 - 2024. All rights reserved.