title: youtube API V3 Search Python
body: <p>I am Trying to use the <a href="https://developers.google.com/youtube/v3/docs/search" rel="nofollow">youtube-api V3 search</a>. I have a flask web application and am trying to output the results into rows on the webpage.</p>
  
  <p>I started with the <a href="https://developers.google.com/youtube/v3/code_samples/python#search_by_keyword" rel="nofollow">Google developers pages</a> (works perfect in terminal) then found <a href="https://github.com/youtube/yt-samples-python-app-engine-search-by-keyword/blob/master/main.py" rel="nofollow">this great sounding example</a> which would output thumbnails and hyper links from the results. However it ran on Google app engine and I am trying to create a Flask application.</p>
  
  <p>So with my very limited knowledge I have tried to merge them to fit my needs. </p>
  
  <pre><code>#!/usr/bin/python
  
  from apiclient.discovery import build
  from apiclient.errors import HttpError
  from oauth2client.tools import argparser
  import sys
  import os
  import urllib
  
  # Set API_KEY to the "API key" value from the "Access" tab of the
  # Google APIs Console http://code.google.com/apis/console#access
  # Please ensure that you have enabled the YouTube Data API and Freebase API
  # for your project.
  API_KEY = "REPLACE ME" #Yes I did replace this with my API KEY
  YOUTUBE_API_SERVICE_NAME = "youtube"
  YOUTUBE_API_VERSION = "v3"
  QUERY_TERM = "dog"
  
  def search_by_keyword():
      youtube = build(
        YOUTUBE_API_SERVICE_NAME, 
        YOUTUBE_API_VERSION, 
        developerKey=API_KEY
      )
      search_response = youtube.search().list(
        q=QUERY_TERM,
        part="id,snippet",
        maxResults=25
      ).execute()
  
      videos = []
  
      for search_result in search_response.get("items", []):
      if search_result["id"]["kind"] == "youtube#video":
              videos.append("%s (%s)" % (search_result["snippet"]["title"],
                                         search_result["id"]["videoId"]))
  
  print "Videos:\n", "\n".join(videos), "\n"
  
  if __name__ == "__main__":
    try:
      youtube_search()
    except HttpError, e:
      print "An HTTP error %d occurred:\n%s" % (e.resp.status, e.content)
  </code></pre>
  
  <p>What I actually want it to do is return results to my webpage rather than just print but thought it was best to take one step at a time. If I can make it work in terminal I could then sort it on a webpage, but not as simple as I thought.</p>
  
  <p>Currently I receive error;</p>
  
  <pre><code>Traceback (most recent call last):
    File "search.py", line 38, in &lt;module&gt;
      print "Videos:\n", "\n".join(videos), "\n"
  NameError: name 'videos' is not defined
  </code></pre>
  
  <p>Which is my first main hurdle as I was expecting it to work as it did in both examples I took it from.</p>
  
  <p>My second problem is how do I go about converting results (assuming I could fix above) into a table on the webpage?</p>
  
  <p>I plan to have</p>
  
  <pre><code>@app.route('/search')
  </code></pre>
  
  <p>include the above code and then;</p>
  
  <pre><code>return render_template ('search.html')
  </code></pre>
  
  <p>But unsure how to pass the results back onto it, do I still need the print for this or is it another term I should be using? I want to be able to use the VideoID returned from each to sort thumbnails and links.</p>