# 訂閱 YouTube Data API 的 Push Notifications ###### tags: `webhook` `youtube` YouTube Data API (v3) 可在頻道發生以下任一活動時透過 [PubSubHubbub](https://github.com/pubsubhubbub/ "An open, simple, web-scale and decentralized pubsub protocol.") 發送 push notification(s): * 上傳一部影片 * 修改一部影片的標題 * 修改一部影片的說明 下列步驟解釋了如何訂閱 push notification(s): 1. 建置可處理接收 Atom feed notifications 的 callback 服務。 2. 到 [Google hub](https://pubsubhubbub.appspot.com/subscribe) 註冊 webhook。 * **Callback URL**:Webhook 的網址。 * **Topic URL**:要接收 push notification(s)的頻道資訊,格式為`https://www.youtube.com/xml/feeds/videos.xml?channel_id=CHANNEL_ID`;**`CHANNEL_ID`** 是**頻道 id**。 * **Mode**:選擇`Subscribe`<sub>(訂閱)</sub>或`Unsubscribe`<sub>(取消訂閱)</sub>。 3. Callback 服務接收到 push notification 的範例格式: ``` <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=CHANNEL_ID"/> <title>YouTube video feed</title> <updated>2015-04-01T19:05:24.552394234+00:00</updated> <entry> <id>yt:video:VIDEO_ID</id> <yt:videoId>VIDEO_ID</yt:videoId> <yt:channelId>CHANNEL_ID</yt:channelId> <title>Video title</title> <link rel="alternate" href="http://www.youtube.com/watch?v=VIDEO_ID"/> <author> <name>Channel title</name> <uri>http://www.youtube.com/channel/CHANNEL_ID</uri> </author> <published>2015-03-06T21:40:57+00:00</published> <updated>2015-03-09T19:05:24.552394234+00:00</updated> </entry> </feed> ``` ## DTO<sup>[](https://martinfowler.com/eaaCatalog/dataTransferObject.html)[](https://en.wikipedia.org/wiki/Data_transfer_object)</sup> 為了簡潔篇幅以下省略 getters 及 setters methods,請自行補上或使用 [Lombok](https://projectlombok.org/)。 ### `/feed/entry/author` element ``` public class Author { @JacksonXmlElementWrapper private String name; @JacksonXmlElementWrapper private String uri; } ``` ### `/feed/entry` element ``` public class Entry { @JacksonXmlElementWrapper private String id; @JacksonXmlElementWrapper(namespace = "yt") private String videoId; @JacksonXmlElementWrapper(namespace = "yt") private String channelId; @JacksonXmlElementWrapper private String title; @JacksonXmlElementWrapper private Link link; @JacksonXmlElementWrapper private Author author; @JacksonXmlElementWrapper private Date published; @JacksonXmlElementWrapper private Date updated; } ``` ### `/feed/link` element(s) ``` public class Link { @JacksonXmlProperty(isAttribute = true) private String rel; @JacksonXmlProperty(isAttribute = true) private String href; } ``` ### `/feed` element ``` public class Feed { @JacksonXmlElementWrapper(useWrapping = false) private List<Link> link = new ArrayList<>(); @JacksonXmlElementWrapper private String title; @JacksonXmlElementWrapper private Date updated; @JacksonXmlElementWrapper private Entry entry; } ```
×
Sign in
Email
Password
Forgot password
or
By clicking below, you agree to our
terms of service
.
Sign in via Facebook
Sign in via Twitter
Sign in via GitHub
Sign in via Dropbox
Sign in with Wallet
Wallet (
)
Connect another wallet
New to HackMD?
Sign up