我正在尝试创建一项服务,将视频上传到我客户的YouTube帐户。
这是我到目前为止所做的:
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需要做什么,需要做出牺牲,还是需要跳舞的跳汰机。我只是难过。