【问题标题】:Uploading videos to YouTube w/ Ruby and v3 API results in Unauthorized error使用 Ruby 和 v3 API 将视频上传到 YouTube 会导致未经授权的错误
【发布时间】:2016-02-28 13:11:03
【问题描述】:

我正在尝试创建一项将视频上传到我客户的 YouTube 帐户的服务。

这是我到目前为止所做的:

  • 我正在使用 Google Ruby gem - googleauthgoogle-api-client - 并设置了“服务帐户”。
  • 我已将 GOOGLE_APPLICATION_CREDENTIALS 环境变量设置为指向设置服务帐户时提供的下载 json 文件
  • 我需要正确的库:google/apis/youtube_v3

代码如下:

class Video < ActiveRecord::Base

  def upload_to_youtube!(force=false)
    if self.youtube_key.blank? || force
      yt = Google::Apis::YoutubeV3::YouTubeService.new
      scopes =  [
        "https://www.googleapis.com/auth/youtube", 
        "https://www.googleapis.com/auth/youtube.force-ssl", 
        "https://www.googleapis.com/auth/youtube.upload"
      ]
      authorization = Google::Auth.get_application_default(scopes)
      yt.authorization = authorization

      title = "Clever Title"
      description = "Lorem ipsum dolor"
      file_path = "/path/to/mp4/file.mp4"

      metadata = {
        snippet: {
          title: title,
          description: description,
        },
      }

      yt.insert_video("id", metadata, upload_source: file_path, content_type: "video/mp4", options: {retries: 3})
    else
      true
    end
  end
end

运行时出现以下错误:

Sending upload start command to https://www.googleapis.com/upload/youtube/v3/videos?part=id
Upload status final
Error - #<Google::Apis::AuthorizationError: Unauthorized>

Retrying after authentication failure
Sending upload query command to
Error - #<NoMethodError: undefined method `+' for nil:NilClass>

NoMethodError: undefined method `+' for nil:NilClass
from /Users/samullen/.rbenv/versions/2.2.2/lib/ruby/2.2.0/net/http.rb:1532:in `addr_port'

我很茫然,我的时间不多了。我不知道 OAuth 是否还有其他需要我做的事情,需要做出的牺牲,或者需要跳舞的 jig。我只是难过。

【问题讨论】:

  • 您最终使用了哪种类型的身份验证?

标签: ruby api youtube google-api-client


【解决方案1】:

问题很可能是由于尝试使用服务帐户而导致出现youtubeSignupRequired 错误。

客户端库中存在导致错误被掩盖的错误,但存在一个问题。

【讨论】:

  • 谢谢,史蒂夫。基于该错误,我是否理解 YouTube 不允许服务帐户进行上传?
  • 这是我的理解。
猜你喜欢
  • 1970-01-01
  • 2013-12-09
  • 1970-01-01
  • 2012-12-23
  • 2014-03-10
  • 2017-06-14
  • 2014-11-26
  • 1970-01-01
  • 2012-10-27
相关资源
最近更新 更多