【发布时间】:2016-02-28 13:11:03
【问题描述】:
我正在尝试创建一项将视频上传到我客户的 YouTube 帐户的服务。
这是我到目前为止所做的:
- 我正在使用 Google Ruby gem -
googleauth和google-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