I am trying to create a button. When someone clicks it will check the status of the user for a youtube channel subscription. I you want to check the subscription status of the user who is clicking the button for my youtube channel.
Using following JS Code:
function checkSubscriptionStatus() { var channelID = 'your_channel_id'; var APIKey = 'your_api_key'; // Get user's Google account ID from YouTube API gapi.client.load('youtube', 'v3', function() { gapi.client.youtube.channels.list({ mine: true, part: 'id' }).then(function(response) { var userID = response.result.items[0].id; // Use the YouTube Data API to retrieve subscription status var url = 'https://www.googleapis.com/youtube/v3/subscriptions'; url += '?part=status&channelId='+ channelID +'&subscriberId='+ userID +'&key='+ APIKey; // Send API request fetch(url) .then(response => response.json()) .then(data => { // Check if the user is subscribed var isSubscribed = data.items[0].status.subscribed; if (isSubscribed) { // User is subscribed, show message or do something else console.log('User is subscribed'); } else { // User is not subscribed, show message or do something else console.log('User is not subscribed'); } }) .catch(error => { console.error('Error checking subscription status', error); }); }); });}
But I am getting following error in my console:
error loading GAPI client for API
Note: I have already included following code in my header file as I adding this button in a WordPress website
<script src="https://apis.google.com/js/api.js"></script><script> function initClient() { gapi.client.init({ apiKey: 'YOUR_API_KEY', discoveryDocs: ['https://www.googleapis.com/discovery/v1/apis/youtube/v3/rest'], }); } gapi.load('client', initClient);</script>
Would love to have some help from the community.