gpt4 book ai didi

python - Youtube-api::通过命令行设置元数据字段

转载 作者:太空宇宙 更新时间:2023-11-04 05:15:37 25 4
gpt4 key购买 nike

很抱歉这个基本问题,但我一直在到处寻找通过 command-line 调用 youtube api 的用法示例,以及如何设置它适当的元数据字段。

以下代码由谷歌提供:Search Youtube by 'keyword'

#!/usr/bin/python

import os
from apiclient.discovery import build
from apiclient.errors import HttpError
from oauth2client.tools import argparser

DEVELOPER_KEY = "mykey"
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)

search_response = youtube.search().list(
q=options.q,
part="id,snippet",
maxResults=options.max_results
).execute()

videos = []
channels = []
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__":
argparser.add_argument("--q", help="Search term", default="Google")
argparser.add_argument("--max-results", help="Max results", default=25)
args = argparser.parse_args()

try:
youtube_search(args)
except HttpError, e:
print "An HTTP error %d occurred:\n%s" % (e.resp.status, e.content)
search.py

我已经按照此处给出的示例进行操作:Youtube>Data API>SampleRequests

例如,搜索 Karma Police 视频,我已经相应地尝试过:

$ python script.py --q="karma police",无济于事。它什么都不打印。

我错过了什么?

ps. 进一步的凭据在位于同一目录的环境 .json 文件 中设置。

编辑:回溯:

Traceback (most recent call last):
File "audio.py", line 64, in <module>
youtube_search(args)
File "audio.py", line 24, in youtube_search
developerKey=DEVELOPER_KEY)
File "/Library/Python/2.7/site-packages/oauth2client/_helpers.py", line 133, in positional_wrapper
return wrapped(*args, **kwargs)
File "/Library/Python/2.7/site-packages/googleapiclient/discovery.py", line 226, in build
credentials=credentials)
File "/Library/Python/2.7/site-packages/oauth2client/_helpers.py", line 133, in positional_wrapper
return wrapped(*args, **kwargs)
File "/Library/Python/2.7/site-packages/googleapiclient/discovery.py", line 358, in build_from_document
credentials = _auth.default_credentials()
File "/Library/Python/2.7/site-packages/googleapiclient/_auth.py", line 41, in default_credentials
return oauth2client.client.GoogleCredentials.get_application_default()
File "/Library/Python/2.7/site-packages/oauth2client/client.py", line 1264, in get_application_default
return GoogleCredentials._get_implicit_credentials()
File "/Library/Python/2.7/site-packages/oauth2client/client.py", line 1249, in _get_implicit_credentials
credentials = checker()
File "/Library/Python/2.7/site-packages/oauth2client/client.py", line 1200, in _implicit_credentials_from_files
credentials_filename = _get_environment_variable_file()
File "/Library/Python/2.7/site-packages/oauth2client/client.py", line 1348, in _get_environment_variable_file
' environment variable) does not exist!')
oauth2client.client.ApplicationDefaultCredentialsError: File “/Users//api/youtube/urlaudio/myproject.json” (pointed by GOOGLE_APPLICATION_CREDENTIALS environment variable) does not exist!

最佳答案

执行以下步骤 provided in the docs .最后,您需要将 GOOGLE_APPLICATION_CREDENTIALS 指向您下载的 json 文件,例如:

export GOOGLE_APPLICATION_CREDENTIALS=~/Downloads/youtube-search-b0e0b347241c.json

当然,您最好将该文件移动到安全的地方,例如进入 ~/.credentials/chmod 700 那个目录。

关于python - Youtube-api::通过命令行设置元数据字段,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41783715/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com