【问题标题】:How to find out if user is subscribed to youtube channel or not如何确定用户是否订阅了 youtube 频道
【发布时间】:2016-09-16 22:23:30
【问题描述】:

我的页面上有 youtube 订阅按钮(iframe)。有什么方法可以知道访问该网站的用户是否已订阅? 如果这不可能,也许有办法知道正在查看该网站的某种用户 ID?

我发现当用户订阅时有一个属性

data-is-subscribed="true" 

对于按钮元素,但由于它来自 youtube 域,我无法以编程方式访问(至少我不知道如何访问)iframe 源。

Blocked a frame with origin https://<my host ip> from accessing a frame with
origin https://<target host ip>. Protocols, domains, and ports must match.

谢谢

【问题讨论】:

  • 它已经过时了,但我想知道。这个问题你解决了吗??

标签: javascript jquery iframe youtube youtube-api


【解决方案1】:

有两种方法可以检索用户的订阅:

  • 要请求当前登录用户订阅的提要,请向以下 URL 发送 GET 请求。注意:对于此请求,您必须提供授权令牌,以便 YouTube 验证用户是否已授权访问资源。

https://gdata.youtube.com/feeds/api/users/default/subscriptions?v=2

*要请求其他用户订阅的提要,请向以下 URL 发送 GET 请求。请注意,此请求不需要用户授权。

https://gdata.youtube.com/feeds/api/users/userId/subscriptions?v=2

在上面的 URL 中,您应该将文本 userId 替换为用户的 YouTube 用户 ID。出于向后兼容的目的,API 还支持指定用户的 YouTube 用户名。

您可以从中获取列表或空响应,这意味着用户没有订阅频道。您可以使用该列表来过滤他们是否订阅。

【讨论】:

    【解决方案2】:

    我将 cookie 与 Youtube 订阅者代码结合使用。

    使用此帖子中的 cookie 代码,Create, read, and erase cookies with jQuery

    我在 Youtube 返回订阅事件时添加 createCookie,在取消订阅事件时添加擦除Cookie,然后是适当的重定向。由于这段代码不是jquery,所以我把这段代码放在jquery ready函数之外。

            function onYtEvent(payload) {
                console.log(payload);
                if (payload.eventType == 'subscribe') {
                    // Add code to handle subscribe event.
                    createCookie('subscribed','yes',30);
                    location.hash = '#mainpage';
                } else if (payload.eventType == 'unsubscribe') {
                    // Add code to handle unsubscribe event.
                    eraseCookie('subscribed');
                    location.hash = '#subscribepage';
                }
                if (window.console) { // for debugging only
                    window.console.log('YT event: ', payload);
                }
            }
    

    然后在jquery的ready函数里面,我添加了readCookie函数

                if (readCookie('subscribed') === 'yes') {
                    location.hash = '#mainpage';
                } else {
                    location.hash = '#subscribepage';
                }
    

    我正在使用 JQM 来处理我的页面重定向。

    我完成的目标是当页面第一次加载时会读取订阅的cookie,如果存在,我知道用户已经订阅,并重定向到主页。

    如果 cookie 不存在,将重定向到显示 youtube 订阅者按钮的订阅者页面。

    因此,用户只需按下订阅按钮一次。 使用 YouTube API 需要用户进行额外的身份验证,并受到配额限制。我想要一个不受这些限制的更简单的解决方案,因此如上所述使用 cookie。

    你可以在http://recipes.quickminutemeals.com查看我的例子

    【讨论】:

    • 是否可以激励订阅点击?希望 YT 不会为此暂停帐户
    【解决方案3】:

    你可以用它来顺利地做到这一点..

    Use the subscriptions#list method and pass mine = true and the channel ID you want to check in forChannelId. If the authenticated user is not subscribed to that channel, it will return an empty list
    

    【讨论】:

    • 问题是这种方法会要求用户授权我的页面供用户访问 youtube 数据,这是我想避免的。
    • 请看看这个问题。This will help you out
    猜你喜欢
    • 2013-09-29
    • 1970-01-01
    • 2014-05-15
    • 2014-09-19
    • 2013-03-16
    • 2021-02-01
    • 2015-10-14
    • 2019-01-18
    • 2016-10-04
    相关资源
    最近更新 更多