如何在Google Colab中使用YouTube API OAUTH 2.0?
我正在尝试在Google Colab中使用YouTube数据API检索搜索结果。
我正在使用代码作为蓝图。
代码:
#!/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)
但是,执行此代码时,我在控制台上收到了此消息:
请访问此URL以授权此应用程序:
点击链接,但是,页面显示此消息/错误:
错误400:redirect_uri_mismatch 请求中的重定向URI,urn:ietf:wg:oauth:2.0:OOB,只能由客户端ID用于本机应用程序。 Web客户端类型不允许使用。您可以在 https://console.developers.google.google上创建一个用于本机应用程序的客户端ID。 com/apis/recertentials/oauthclient
屏幕截图:
我无法找到任何在Google Colab中使用OAuth 2.0的文档或教程。
我发现此答案 - 使用Google Oauth 2.0 Playground--但与Google Colab和错误400--我认为是关于IPS/Permissons在API_KEY上设置的有关某些域的。
我还找到了这个问题我检查并尝试了在那里做出的答案,但是,问题仍然存在,我说,我说的是相信不可能将OAuth 2.0与Google Colab一起使用 - 我希望在这种情况下我错了...
这个答案说:
要解决您的问题,您必须在Google Developers Console中的项目上以及在Python脚本中设置相同的重定向URI。
。
我添加了Google Colagoratory的领域,并且错误仍在继续。有人知道这个问题的正确步骤吗?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您需要获取开发人员密钥并替换在此处所说的内容,以授权API连接,就像本节中的突出显示:
You need to get the developer key and replace like how it says here to authorize the API connection like highlighted in this section: