抱歉,这是一个基本问题,但是我到处都在寻找通过youtube api
调用command-line
的用法示例以及如何设置其正确的元数据字段的示例。
以下代码由Google在此处提供: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 file
中设置了更多凭据。编辑:追溯:
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/