使用 Ruby 和 v3 API 将视频上传到 YouTube 会导致未经授权的错误
Uploading videos to YouTube w/ Ruby and v3 API results in Unauthorized error
我正在尝试创建一项服务,将视频上传到我客户的 YouTube 帐户。
这是我到目前为止所做的:
- 我正在使用 Google Ruby gem -
googleauth
和 google-api-client
- 并设置了 "service account"。
- 我已将
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 做其他事情,需要做出牺牲,或者需要跳舞。我只是被难住了。
问题很可能是您在尝试使用服务帐户时收到 youtubeSignupRequired 错误。
客户端库中存在导致错误被屏蔽的错误,但有一个未解决的问题。
我正在尝试创建一项服务,将视频上传到我客户的 YouTube 帐户。
这是我到目前为止所做的:
- 我正在使用 Google Ruby gem -
googleauth
和google-api-client
- 并设置了 "service account"。 - 我已将
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 做其他事情,需要做出牺牲,或者需要跳舞。我只是被难住了。
问题很可能是您在尝试使用服务帐户时收到 youtubeSignupRequired 错误。
客户端库中存在导致错误被屏蔽的错误,但有一个未解决的问题。