MODX Community

getYouTube error

I’m trying to implement the getYouTube Extra, but I can’t get it to show anything. After a look in the error logs, this comes up:

[2019-07-03 00:30:58] (ERROR @ ***/core/components/getyoutube/model/getyoutube/search.class.php : 63) PHP warning: Invalid argument supplied for foreach()

The respective code in the search.class.php is:

foreach($videos['items'] as $video) {
  /* SET PLACEHOLDERS */
  $this->modx->setPlaceholder('id',$video['id']['videoId']);
  $this->modx->setPlaceholder('url',"https://www.youtube.com/watch?v=" . $video['id']['videoId']);
  $this->modx->setPlaceholder('embed_url',"https://www.youtube.com/embed/" . $video['id']['videoId']);
  $this->modx->setPlaceholder('title',$video['snippet']['title']);
  $this->modx->setPlaceholder('channel_title',$video['snippet']['channelTitle']);
  $this->modx->setPlaceholder('description',$video['snippet']['description']);
  $this->modx->setPlaceholder('publish_date',$video['snippet']['publishedAt']);
  $this->modx->setPlaceholder('thumbnail_small',$video['snippet']['thumbnails']['default']['url']); //120px wide and 90px tall
  $this->modx->setPlaceholder('thumbnail_medium',$video['snippet']['thumbnails']['medium']['url']); //320px wide and 180px tall
  $this->modx->setPlaceholder('thumbnail_large',$video['snippet']['thumbnails']['high']['url']); //480px wide and 360px tall
  /* SET TEMPLATES */
  if (!empty($tplAlt)) {
	if($idx % 2 == 0) { // Checks if index can be divided by 2 (alt)
	  $rowTpl = $tpl;
	}else{
	  $rowTpl = $tplAlt;
	}
  }else{
	$rowTpl = $tpl;
  }
  $idx++; //Increases index by +1

  $results .= $this->modx->getChunk($rowTpl,$video);
}

Can someone tell me what the error is referring to?

It would seem that the $videos['items'] array is empty. Did you acquire your YouTube API key?

1 Like

Yes, I went to the Google Developers Console, activated the YouTube Data API v3, created new credentials and assigned them to the API. I’ve tried both, Browser and Server Keys so far, without luck.

As far as I understand this, I don’t need to create an OAuth 2.0 Key, right? I only want to fetch videos from a channel, so no personal data is involved. I basically use the code from the docs:

[[!getYoutube? 
   &mode=`channel` 
   &channel=`UCBR8-60-B28hp2BmDPdntcQ` 
   &tpl=`videoTpl`
   &limit=`10`
]]
					    
[[+prevPage:notempty=`<a href="[[+prevPage]]">prevPage</a><br>`]]
[[+nextPage:notempty=`<a href="[[+nextPage]]">nextPage</a><br>`]]



// videoTpl:

<div>
  <span>[[+idx]]<a href="[[+url]]" target="_blank">[[+title]]</a></span><br>
  <a href="[[+url]]" target="_blank"><img src="[[+thumbnail_small]]" alt="[[+title]]"></a><br>
  <span><i>[[+publish_date:ago]]</i><br></span>
</div>

Yesterday I was atleast getting error codes in the metrics view of the Google Developers Console, I think those were 400 & 403 response codes, but I don’t seem to get them now, but maybe that’s a hint?

Well, I just tried it with a new API Key (which I did a bunch of times before) and it worked. I really have no clue what’s different now, but it works!

Edit: I think now, the issue was probably due to wrong set Key restrictions, probably something with the HTTP referrer, to be exact.

This topic was automatically closed 2 days after the last reply. New replies are no longer allowed.