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

Youtube-api :: setting metadata fields via command-line

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

以下代码由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 警察视频,我已经尝试过,因此:

$ 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 那个目录。