Youtube API - Push Notification Subscription

My ultimate goal is to set up a Webhook when a YouTube user uploads a video. After some research, I got this article .

But when I get to the https://www.youtube.com/xml/feeds/videos.xml?channel_id=CHANNEL_ID part, I got a Restricted topic error when trying to subscribe to Google / SuperFeedr hubs. I also got the callback url.

The topic I want to subscribe to is: https://www.youtube.com/xml/feeds/videos.xml?channel_id=UC7T8roVtC_3afWKTOGtLlBA

What is not displayed when visiting through a browser.

Am I doing something wrong? I struggled for hours, any help is appreciated. Thanks!

UPDATE : I found this one , but these feeds do not have the rel="hub" attribute, so it’s probably useless if I want to subscribe to a hub.

+6
source share
3 answers
  • you need to send a request to subscribe to the channel
  • callback that modifies a subscription request and receives actual update data

subscribe to the function:

subscribe.php might look like this:

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

after sending the request, the pusub service will call youtube_subscribe_callback.php to confirm the subscription, it will use the GET method and it expects to receive a response that is "hub_challenge". after that, if you upload the video to your test channel, youtube_subscribe_callback.php will receive a POST request with the data.

so youtube_subscribe_callback.php (defined in the subscribeYoutubeChannel function) might look like this:

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

I could not subscribe to the channel by id, but I was able to do this by username:

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

So go to this page:

 https://pubsubhubbub.appspot.com/subscribe 

Paste the callback url, youtube RSS feed with username and subscribe mode.

Remember to answer from your callback URL so that it can confirm your subscription, in PHP just print:

 echo $_REQUEST["hub_challenge"]; 

More details here and here .

+2
source

The process usually consists of two steps: first, you go to the subscription page, enter the URL of the callback server, the URL of the topic (which is basically the URL of the channel for the ytb channel that you want to listen to, other fields are optional), the server the pub will check your GET subscription with a request to your callback server, on the go it might look like this:

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

Then, with each new video (or title, the desc of the old video is updated), the pub will send a POST request to your server with xml in a body similar to this:

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

Source: https://habr.com/ru/post/985081/


All Articles