Quantcast
Channel: Active questions tagged youtube-api - Stack Overflow
Viewing all articles
Browse latest Browse all 3638

Why do I get an invalid API key error when trying to call the YouTube API?

$
0
0

I am trying to call the YouTube API to get the most recent and most popular videos from my channel and display them on my website. My code worked before, but then randomly stopped working out of the blue. I already have my YouTube API key enabled and I have tried using multiple different API keys but it will still not work.

Here is the code I am trying to use.

<?php$APIkey = "myAPIkey";$channelID = "UC4AHDpDN38KZRgb16VhaiaA";$maxResults = 1;$apiDataPopular = @file_get_contents('https://www.googleapis.com/youtube/v3/search?order=date&part=snippet&channelId='.$channelID.'&maxResults='.$maxResults.'&key='.$APIkey.'&order=viewcount');$apiDataRecent = @file_get_contents('https://www.googleapis.com/youtube/v3/search?order=date&part=snippet&channelId='.$channelID.'&maxResults='.$maxResults.'&key='.$APIkey.'');    if($apiDataPopular)    {        $videoListPopular = json_decode($apiDataPopular);    }    else    {        echo "Invalid API key or Channel ID";    }    if($apiDataRecent)    {        $videoListRecent = json_decode($apiDataRecent);    }    else    {        echo "Invalid API key or Channel ID";    }?>

Then in my HTML where I want the videos displayed, I use this code.

<div class="newestVid"><h2>Newest Video</h2><?php    if(!empty($videoListRecent->items)){     foreach($videoListRecent->items as $item){         // Embed video         if(isset($item->id->videoId)){             echo '<iframe width="100%" height="50%" src="https://www.youtube.com/embed/'.$item->id->videoId.'" frameborder="0" allowfullscreen></iframe>';         }     }    }     else    {         echo '<p style="color:red">ERRORRRR</p>';     }    ?></div><div class="popularVid"><h2>Most Viewed Video</h2><?php    if(!empty($videoListPopular->items))    {             foreach($videoListPopular->items as $item)            {             // Embed video                 if(isset($item->id->videoId))                {                     echo '<iframe width="100%" height="50%" src="https://www.youtube.com/embed/'.$item->id->videoId.'" frameborder="0" allowfullscreen></iframe>';                 }             }    }    else    {         echo '<p style="color:red">ERROR</p>';     }     ?></div>

My code used to display the videos, but now it only displays the errors I included in my else statements.


Viewing all articles
Browse latest Browse all 3638

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>