Reputation: 434
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
Upvotes: 4
Views: 8796
Reputation: 776
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 accomplished 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.com
Upvotes: 0
Reputation: 6791
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.com/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.com/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 compatibility 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.
Upvotes: 2
Reputation: 13
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
Upvotes: -1