javascript - How to find out if user is subscribed to youtube channel or not - Stack Overflow

I've youtube subscribe button(iframe) on my page. Is there any way to know whether user accessing

I've youtube subscribe button(iframe) on my page. Is there any way to know whether user accessing the site is subscribed or not? If that's not possible, maybe there's a way to know some sort of user id who's viewing the site?

I found that when user is subscribed there's an attribute

data-is-subscribed="true" 

for button element, but since it's from youtube domain I can't access (at least i didn't found out how to) iframes source programatically.

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.

Thanks

I've youtube subscribe button(iframe) on my page. Is there any way to know whether user accessing the site is subscribed or not? If that's not possible, maybe there's a way to know some sort of user id who's viewing the site?

I found that when user is subscribed there's an attribute

data-is-subscribed="true" 

for button element, but since it's from youtube domain I can't access (at least i didn't found out how to) iframes source programatically.

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.

Thanks

Share Improve this question asked May 19, 2016 at 20:26 arvistarvist 4341 gold badge5 silver badges9 bronze badges 1
  • it's outdated but i want to know. Have you solved this problem?? – Muhammad Usman Commented Apr 7, 2021 at 1:33
Add a ment  | 

3 Answers 3

Reset to default 2

There are two ways to Retrieving a user's subscriptions:

  • To request a feed of the currently logged-in user's subscriptions, send a GET request to the following URL. Note: For this request, you must provide an authorization token, which enables YouTube to verify that the user authorized access to the resource.

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

*To request a feed of another user's subscriptions, send a GET request to the following URL. Note that this request does not require user authorization.

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

In the URL above, you should replace the text userId with the user's YouTube user ID. For backward patibility purposes, the API also supports having the user's YouTube username specified instead.

From this you can get the list or a null response that means the user has no subscribed channel. You can use that list to filter if they are subscribed or not.

I am using a cookie in conjunction with Youtube subscriber code.

Using the cookie code from this posting, Create, read, and erase cookies with jQuery

I add createCookie when Youtube returns a subscribe event, and eraseCookie when an unsubscriber event, followed by th approproate redirect. Since this code is not jquery, I place this code outside the jquery ready function.

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

then inside the jquery ready function, I add the readCookie function

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

I am using JQM to handle my page redirections.

The goal I acplished is when the page first loads will read the subscribed cookie, if present, I know the user has subscribed, and redirect to the mainpage.

If the cookie does not exist, will redirect to the subscriber page showing the youtube subscriber button.

Therefore, the user only needs to press the subscribe button once. Using the YouTube API require additional authentication by the user, and subject to quota limits. I wanted a simpler solution not subject to these limits, so cookies are use as described above.

You can check my example at http://recipes.quickminutemeals.

You can use this to do that smoothly..

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

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745358861a4624267.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信