I'm trying to use YouTube Data API in Google Colab for retrieve the results of a search.
I'm using the code available in the documentation as a blueprint.
Code:
#!/usr/bin/python
# This sample executes a search request for the specified search term.
# Sample usage:
# python search.py --q=surfing --max-results=10
# NOTE: To use the sample, you must provide a developer key obtained
# in the Google APIs Console. Search for "REPLACE_ME" in this code
# to find the correct place to provide that key..
import argparse
from googleapiclient.discovery import build
from googleapiclient.errors import HttpError
# Set DEVELOPER_KEY to the API key value from the APIs & auth > Registered apps
# tab of
# https://cloud.google.com/console
# Please ensure that you have enabled the YouTube Data API for your project.
DEVELOPER_KEY = 'REPLACE_ME'
YOUTUBE_API_SERVICE_NAME = 'youtube'
YOUTUBE_API_VERSION = 'v3'
def youtube_search(options):
youtube = build(YOUTUBE_API_SERVICE_NAME, YOUTUBE_API_VERSION,
developerKey=DEVELOPER_KEY)
# Call the search.list method to retrieve results matching the specified
# query term.
search_response = youtube.search().list(
q=options.q,
part='id,snippet',
maxResults=options.max_results
).execute()
videos = []
channels = []
playlists = []
# Add each result to the appropriate list, and then display the lists of
# matching videos, channels, and playlists.
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']))
elif search_result['id']['kind'] == 'youtube#channel':
channels.append('%s (%s)' % (search_result['snippet']['title'],
search_result['id']['channelId']))
elif search_result['id']['kind'] == 'youtube#playlist':
playlists.append('%s (%s)' % (search_result['snippet']['title'],
search_result['id']['playlistId']))
print 'Videos:\n', '\n'.join(videos), '\n'
print 'Channels:\n', '\n'.join(channels), '\n'
print 'Playlists:\n', '\n'.join(playlists), '\n'
if __name__ == '__main__':
parser = argparse.ArgumentParser()
parser.add_argument('--q', help='Search term', default='Google')
parser.add_argument('--max-results', help='Max results', default=25)
args = parser.parse_args()
try:
youtube_search(args)
except HttpError, e:
print 'An HTTP error %d occurred:\n%s' % (e.resp.status, e.content)
However, when executing this code, I got this message on the console:
Please visit this URL to authorize this application: https://accounts.google.com/o/oauth2/auth?response_type=code&client_id=327114017303-uvhp1oeqmuvsjohr0bmjevcet3g6tsb6.apps.googleusercontent.com&redirect_uri=urn%3Aietf%3Awg%3Aoauth%3A2.0%3Aoob&scope=https%3A%2F%2Fwww.googleapis.com%2Fauth%2Fyoutube+https%3A%2F%2Fwww.googleapis.com%2Fauth%2Fyoutube.readonly+https%3A%2F%2Fwww.googleapis.com%2Fauth%2Fyoutube.force-ssl&state=qUtm4Dd4xh7NPerHBdf8bsEP3yoae2&prompt=consent&access_type=offline
I clicked on the link, but, the page shows this message/error:
Error 400: redirect_uri_mismatch The redirect URI in the request, urn:ietf:wg:oauth:2.0:oob, can only be used by a Client ID for native application. It is not allowed for the WEB client type. You can create a Client ID for native application at https://console.developers.google.com/apis/credentials/oauthclient
Screenshot:
I'm unable to find any documentation or tutorial that shows how to use OAuth 2.0 in Google Colab.
I find this answer - which use the Google OAuth 2.0 Playground -, but, nothing related to Google Colab and the Error 400 - which I think it's about the IPs/permissons to set on the API_KEY about certains domains.
I also found this question and I checked and tried the answers made there, but, the problem persist, I'm stating to believe that it's not possible to use OAuth 2.0 with Google Colab - I hope I'm wrong on this case...
This answer says:
To fix you issue, you'll have to have the same redirect URI set on your project within Google developers console and, at the same time, within your Python script.
I have added the domain of Google Colaboratory and the error continues. Does anybody knows the correct steps for solvie this issue?