2016-05-19 118 views
2

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

我发现,当用户订阅有一个为按钮元素的属性

data-is-subscribed="true" 

,但由于它从YouTube域的我无法访问(至少我没有找到如何)I帧的源编程。

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. 

感谢

回答

0

为“我的应用程序监听按钮,您可以用它来做到这一点顺利..

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 
+1

检查我的例子的问题是,这种做法将要求用户授权我的页面的YouTube用户的数据访问,这件事情我想避免的。 – ataurenis

+0

请看看这个问题.. [这会帮助你](http://stackoverflow.com/questions/28189354/get-youtube-channel-subscribers-via-youtube-api) –

0

转到https://developers.google.com/youtube/youtube_subscribe_button

配置您的按钮,然后按复选框事件“。

然后,代码样本中,会有一个代码块,看起来像这样:

function onYtEvent(payload) { 
    if (payload.eventType == 'subscribe') { 
    // Add code to handle subscribe event. 
    } else if (payload.eventType == 'unsubscribe') { 
     // Add code to handle unsubscribe event. 
    } 
    if (window.console) { // for debugging only 
     window.console.log('YT event: ', payload); 
    } 
} 

把你的代码时的认购情况下,如果块用户subscibes。

+0

这只会被调用当用户按下(联合国)订阅按钮。我想知道当用户进入页面时是否订阅了用户。 – ataurenis

1

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

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

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

*要请求其他用户的订阅的饲料,发送GET请求发送到以下网址。请注意,此请求不需要用户授权。

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

在上面的网址,您应该替换的文字userId与用户的YouTube用户ID。为了向后兼容,API还支持指定用户的YouTube用户名。

由此您可以获得列表或空回复,表示用户没有订阅频道。您可以使用该列表过滤订阅与否。

0

我正在使用cookie和Youtube用户代码一起使用。

使用此发布cookie的代码,Create, read, and erase cookies with jQuery

我想补充的createCookie YouTube何时返回订阅事件,eraseCookie当退订事件,其次是日approproate重定向。由于这段代码不是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