【问题标题】:How do I upload a video to Youtube directly from my server?如何直接从我的服务器上传视频到 Youtube?
【发布时间】:2015-05-28 08:49:08
【问题描述】:

我正在设置一个(无头)网络服务器,让人们可以制作自己的自定义延时电影。

有几个人想将他们制作的延时视频上传到 YouTube。 与其将视频下载到那个人的笔记本电脑上, 然后那个人手动将其上传到 YouTube, 有没有办法可以在我的网络服务器上编写一些软件来获取我的网络服务器上的视频文件并将其直接上传到该用户在 YouTube 上的帐户?

有人告诉我,向我的用户询问他们的 YouTube 句柄和密码是错误的做法,我应该使用带有 Oauth 的 YouTube V3 API。

我尝试了以下所列的技术 "I want to upload a video from my web page to youtube by using javascript youtube API", 这似乎“有效”,但每次我都必须将视频下载到那个人的笔记本电脑,然后从笔记本电脑上传到 YouTube。有没有办法调整该系统以直接从我的服务器上传到 YouTube?

我发现了一些 python 代码(在我设置了我的 client_secrets.json 之后),我可以将视频直接从我的服务器直接上传到某人的 YouTube 帐户该人进行了 Oauth 身份验证。 但是,当某个新人第一次尝试将视频上传到我的服务器以前从未处理过的某个新 YouTube 帐户时,它要么 (a) 在我的服务器上打开一个网络浏览器,然后如果我 VNC 到服务器并在该网络浏览器中输入 YouTube 句柄和密码,它就会得到身份验证——但我不想为每个用户都这样做。 (b) 使用“--noauth_local_webserver”选项,在命令行中输出一个 URL 并等待。然后,如果我手动复制该 URL 并将其粘贴到 Web 浏览器中,登录到 YouTube,将令牌复制并粘贴回仍在等待命令行输入的应用程序中,该人将获得身份验证。但我不想对每个用户都这样做。我想如果我可以在我的 cgi-bin 脚本中捕获该 URL 并将其粘贴到网页中,然后以某种方式获取身份验证响应并将其塞回该程序中,那将是可以的,但是如何?我什至没有在这段代码中看到 print 语句或 raw_input 语句。

#!/usr/bin/python
#   https://developers.google.com/youtube/v3/code_samples/python#upload_a_video
# which is identical to the code sample at
#   https://developers.google.com/youtube/v3/docs/videos/insert

import httplib
import httplib2
import os
import random
import sys
import time

from apiclient.discovery import build
from apiclient.errors import HttpError
from apiclient.http import MediaFileUpload
from oauth2client.client import flow_from_clientsecrets
from oauth2client.file import Storage
from oauth2client.tools import argparser, run_flow


# Explicitly tell the underlying HTTP transport library not to retry, since
# we are handling retry logic ourselves.
httplib2.RETRIES = 1

# Maximum number of times to retry before giving up.
MAX_RETRIES = 10

# Always retry when these exceptions are raised.
RETRIABLE_EXCEPTIONS = (httplib2.HttpLib2Error, IOError, httplib.NotConnected,
  httplib.IncompleteRead, httplib.ImproperConnectionState,
  httplib.CannotSendRequest, httplib.CannotSendHeader,
  httplib.ResponseNotReady, httplib.BadStatusLine)

# Always retry when an apiclient.errors.HttpError with one of these status
# codes is raised.
RETRIABLE_STATUS_CODES = [500, 502, 503, 504]

# The CLIENT_SECRETS_FILE variable specifies the name of a file that contains
# the OAuth 2.0 information for this application, including its client_id and
# client_secret. You can acquire an OAuth 2.0 client ID and client secret from
# the Google Developers Console at
# https://console.developers.google.com/.
# Please ensure that you have enabled the YouTube Data API for your project.
# For more information about using OAuth2 to access the YouTube Data API, see:
#   https://developers.google.com/youtube/v3/guides/authentication
# For more information about the client_secrets.json file format, see:
#   https://developers.google.com/api-client-library/python/guide/aaa_client_secrets
CLIENT_SECRETS_FILE = "client_secrets.json"

# This OAuth 2.0 access scope allows an application to upload files to the
# authenticated user's YouTube channel, but doesn't allow other types of access.
YOUTUBE_UPLOAD_SCOPE = "https://www.googleapis.com/auth/youtube.upload"
YOUTUBE_API_SERVICE_NAME = "youtube"
YOUTUBE_API_VERSION = "v3"

# This variable defines a message to display if the CLIENT_SECRETS_FILE is
# missing.
MISSING_CLIENT_SECRETS_MESSAGE = """
WARNING: Please configure OAuth 2.0

To make this sample run you will need to populate the client_secrets.json file
found at:

   %s

with information from the Developers Console
https://console.developers.google.com/

For more information about the client_secrets.json file format, please visit:
https://developers.google.com/api-client-library/python/guide/aaa_client_secrets
""" % os.path.abspath(os.path.join(os.path.dirname(__file__),
                                   CLIENT_SECRETS_FILE))

VALID_PRIVACY_STATUSES = ("public", "private", "unlisted")


def get_authenticated_service(args):
  flow = flow_from_clientsecrets(CLIENT_SECRETS_FILE,
    scope=YOUTUBE_UPLOAD_SCOPE,
    message=MISSING_CLIENT_SECRETS_MESSAGE)

  storage = Storage("%s-oauth2.json" % sys.argv[0])
  credentials = storage.get()

  if credentials is None or credentials.invalid:
    credentials = run_flow(flow, storage, args)

  return build(YOUTUBE_API_SERVICE_NAME, YOUTUBE_API_VERSION,
    http=credentials.authorize(httplib2.Http()))

def initialize_upload(youtube, options):
  tags = None
  if options.keywords:
    tags = options.keywords.split(",")

  body=dict(
    snippet=dict(
      title=options.title,
      description=options.description,
      tags=tags,
      categoryId=options.category
    ),
    status=dict(
      privacyStatus=options.privacyStatus
    )
  )

  # Call the API's videos.insert method to create and upload the video.
  insert_request = youtube.videos().insert(
    part=",".join(body.keys()),
    body=body,
    # The chunksize parameter specifies the size of each chunk of data, in
    # bytes, that will be uploaded at a time. Set a higher value for
    # reliable connections as fewer chunks lead to faster uploads. Set a lower
    # value for better recovery on less reliable connections.
    #
    # Setting "chunksize" equal to -1 in the code below means that the entire
    # file will be uploaded in a single HTTP request. (If the upload fails,
    # it will still be retried where it left off.) This is usually a best
    # practice, but if you're using Python older than 2.6 or if you're
    # running on App Engine, you should set the chunksize to something like
    # 1024 * 1024 (1 megabyte).
    media_body=MediaFileUpload(options.file, chunksize=-1, resumable=True)
  )

  resumable_upload(insert_request)

# This method implements an exponential backoff strategy to resume a
# failed upload.
def resumable_upload(insert_request):
  response = None
  error = None
  retry = 0
  while response is None:
    try:
      print "Uploading file..."
      status, response = insert_request.next_chunk()
      if 'id' in response:
        print "Video id '%s' was successfully uploaded." % response['id']
      else:
        exit("The upload failed with an unexpected response: %s" % response)
    except HttpError, e:
      if e.resp.status in RETRIABLE_STATUS_CODES:
        error = "A retriable HTTP error %d occurred:\n%s" % (e.resp.status,
                                                             e.content)
      else:
        raise
    except RETRIABLE_EXCEPTIONS, e:
      error = "A retriable error occurred: %s" % e

    if error is not None:
      print error
      retry += 1
      if retry > MAX_RETRIES:
        exit("No longer attempting to retry.")

      max_sleep = 2 ** retry
      sleep_seconds = random.random() * max_sleep
      print "Sleeping %f seconds and then retrying..." % sleep_seconds
      time.sleep(sleep_seconds)

if __name__ == '__main__':
  argparser.add_argument("--file", required=True, help="Video file to upload")
  argparser.add_argument("--title", help="Video title", default="Test Title")
  argparser.add_argument("--description", help="Video description",
    default="Test Description")
  argparser.add_argument("--category", default="22",
    help="Numeric video category. " +
      "See https://developers.google.com/youtube/v3/docs/videoCategories/list")
  argparser.add_argument("--keywords", help="Video keywords, comma separated",
    default="")
  argparser.add_argument("--privacyStatus", choices=VALID_PRIVACY_STATUSES,
    default=VALID_PRIVACY_STATUSES[0], help="Video privacy status.")
  args = argparser.parse_args()

  if not os.path.exists(args.file):
    exit("Please specify a valid file using the --file= parameter.")

  youtube = get_authenticated_service(args)
  try:
    initialize_upload(youtube, args)
  except HttpError, e:
    print "An HTTP error %d occurred:\n%s" % (e.resp.status, e.content)

【问题讨论】:

  • 我面临同样的问题。你有什么解决办法吗?请分享。

标签: api oauth youtube


【解决方案1】:

使用“client_secrets.json”

配置凭据以生成它 https://console.developers.google.com/apis/credentials

{
    "web":
    {
        "client_id":"xxxxxxxxxxxxxx",
        "project_id":"xxxxxxxxxxxxxx",
        "auth_uri":"https://accounts.google.com/o/oauth2/auth",
        "token_uri":"https://accounts.google.com/o/oauth2/token",
        "auth_provider_x509_cert_url":"https://www.googleapis.com/oauth2/v1/certs",
        "client_secret":"xxxxxxxxxxxxxxxx",
        "redirect_uris":["http://localhost:8090/","http://localhost:8090/Callback"],
        "javascript_origins":["http://localhost"]
    }
}

【讨论】:

  • developers.google.com/youtube/v3/guides/authentication 服务帐户流支持不访问用户信息的服务器到服务器交互。但是,YouTube 数据 API 不支持此流程。由于无法将服务帐户链接到 YouTube 帐户,因此尝试使用此流程授权请求将生成 NoLinkedYouTubeAccount 错误。
  • @CodingElements 这与这个答案有什么关系?这是 OAuth 不是服务帐户
【解决方案2】:

非常有用的分步指南,介绍如何使用 YouTube OAuth API v3 获取访问权限和新令牌并将其保存以供将来使用。 PHP 服务器端 YouTube V3 OAuth API 视频上传指南。 https://www.domsammut.com/code/php-server-side-youtube-v3-oauth-api-video-upload-guide

【讨论】:

  • 看起来这是基于一个非常旧的 API 版本,它不再工作了。
  • 这是当前版本的 YouTube API v3 developers.google.com/youtube/v3/getting-started
  • 是的,但是没有关于如何从服务器实际上传的信息(因此这个问题)。我刚刚放弃了,现在告诉我的客户改用 Vimeo。
  • @CpnCrunch 这里有一个非常直接简单的脚本来使用codecanyon.net/item/youtube-video-uploader-30/15413440?s_rank=3
  • Ams:该脚本从浏览器上传,而不是从服务器上传。从浏览器上传非常容易——谷歌提供了示例代码,您无需为此付费。没有用于服务器到 youtube 上传的有效示例代码(无需用户干预)。
猜你喜欢
  • 2012-06-30
  • 1970-01-01
  • 2014-11-06
  • 1970-01-01
  • 2016-01-21
  • 2015-10-22
  • 2014-10-17
  • 2020-07-15
  • 1970-01-01
相关资源
最近更新 更多