GeekyGeek
GeekyGeek

Reputation: 21

Error accessing Youtube Data API using Python

I am following the tutorial found here https://www.geeksforgeeks.org/youtube-data-api-set-1/. After I run the below code, I am getting a "No module named 'apiclient'" error. I also tried using "from googleapiclient import discovery" but that gave an error as well. Does anyone have alternatives I can try out?

I have already imported pip install --upgrade google-api-python-client

Would appreciate any help/suggestions!

Here is the code:

from apiclient.discovery import build 

# Arguments that need to passed to the build function 
DEVELOPER_KEY = "your_API_Key"
YOUTUBE_API_SERVICE_NAME = "youtube"
YOUTUBE_API_VERSION = "v3"

# creating Youtube Resource Object 
youtube_object = build(YOUTUBE_API_SERVICE_NAME, YOUTUBE_API_VERSION, 
                                        developerKey = DEVELOPER_KEY) 


def youtube_search_keyword(query, max_results): 

    # calling the search.list method to 
    # retrieve youtube search results 
    search_keyword = youtube_object.search().list(q = query, part = "id, snippet", 
                                            maxResults = max_results).execute() 

    # extracting the results from search response 
    results = search_keyword.get("items", []) 

    # empty list to store video, 
    # channel, playlist metadata 
    videos = [] 
    playlists = [] 
    channels = [] 

    # extracting required info from each result object 
    for result in results: 
        # video result object 
        if result['id']['kind'] == "youtube# video": 
            videos.append("% s (% s) (% s) (% s)" % (result["snippet"]["title"], 
                            result["id"]["videoId"], result['snippet']['description'], 
                            result['snippet']['thumbnails']['default']['url'])) 

        # playlist result object 
        elif result['id']['kind'] == "youtube# playlist": 
            playlists.append("% s (% s) (% s) (% s)" % (result["snippet"]["title"], 
                                result["id"]["playlistId"], 
                                result['snippet']['description'], 
                                result['snippet']['thumbnails']['default']['url'])) 

        # channel result object 
        elif result['id']['kind'] == "youtube# channel": 
            channels.append("% s (% s) (% s) (% s)" % (result["snippet"]["title"], 
                                result["id"]["channelId"], 
                                result['snippet']['description'], 
                                result['snippet']['thumbnails']['default']['url'])) 

    print("Videos:\n", "\n".join(videos), "\n") 
    print("Channels:\n", "\n".join(channels), "\n") 
    print("Playlists:\n", "\n".join(playlists), "\n") 

if __name__ == "__main__": 
    youtube_search_keyword('Geeksforgeeks', max_results = 10) 

Upvotes: 1

Views: 351

Answers (2)

TalkVideo Network
TalkVideo Network

Reputation: 91

Try the YouTube docs here:

https://developers.google.com/youtube/v3/code_samples

They worked for me on a recently updated Slackware_64 14.2

I use them with Python 3.8. Since there may also be a version 2 of Python installed, I make sure to use this in the Interpreter line:

!/usr/bin/python3.8

Likewise with pip, I use pip3.8 to install dependencies

I installed Python from source. python3.8 --version Python 3.8.2

You can also look at this video here:

https://www.youtube.com/watch?v=qDWtB2q_09g

It sort of explains how to use YouTube's API explorer. You can copy code samples directly from there. The video above covers Android, but the same concept applies to Python regarding using YouTube's API Explorer.

I concur with the previous answer regarding version control.

Upvotes: 0

ex4
ex4

Reputation: 2428

With this information it's hard to say what is the problem. But sometimes I've been banging my head to wall when installing something with pip (Python2) and then trying to import module in Python3 or vice versa.

So if you are running your script with Python3, try install package by using pip3 install --upgrade google-api-python-client

Upvotes: 1

Related Questions