代码之家  ›  专栏  ›  技术社区  ›  Bilal Naseer

用于检查我频道的youtube订阅状态的按钮

  •  0
  • Bilal Naseer  · 技术社区  · 2 年前

    我正在尝试创建一个按钮。当有人点击它时,它会检查用户订阅youtube频道的状态。我你想检查用户的订阅状态谁是点击按钮为我的youtube频道。

    使用以下JS代码:

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

    但我在控制台中出现以下错误:

    error loading GAPI client for API
    

    注意:当我在WordPress网站中添加这个按钮时,我已经在头文件中包含了以下代码

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

    希望得到社区的帮助。

    0 回复  |  直到 2 年前