使用 Youtube API v3 ,可以从服务器中生成access_code
的浏览器上传视频。
在 RoR 上运行的网络服务器有客户端证书,使用
进行身份验证Google::APIClient::JWTAsserter
我可以获得对access_code
有效的one hour
。
我还有一个开发人员密钥,可用于基于浏览器的上传的应用。
我想与浏览器分享access_code
,以便用户无需使用他/她的Google帐户登录。
为了在单个脚本中模拟这个,我在ruby中使用了RestClient
。
以下是我根据文档尝试的示例。
require 'google/api_client'
require 'nokogiri'
require 'rest_client'
client = Google::APIClient.new
youtube_service = client.discovered_api('youtube', 'v3')
key = Google::APIClient::PKCS12.load_key('keyfile.p12', 'password')
service_account = Google::APIClient::JWTAsserter.new(
'certificate_email',
'https://www.googleapis.com/auth/youtube.upload',
key)
client.authorization = service_account.authorize
access_token = client.authorization.access_token
dev_key = "developer_key"
upload_metadata_xml = <<-EOF
<?xml version="1.0"?>
<entry xmlns="http://www.w3.org/2005/Atom"
xmlns:media="http://search.yahoo.com/mrss/"
xmlns:yt="http://gdata.youtube.com/schemas/2007">
<media:group>
<media:title type="plain">Test upload one</media:title>
<media:description type="plain">
I gave a bad toast at my friend's wedding.
</media:description>
<media:category
scheme="http://gdata.youtube.com/schemas/2007/categories.cat">People
</media:category>
<media:keywords>upload, test, one</media:keywords>
</media:group>
</entry>
EOF
headers = {
"Authorization" => "Bearer #{access_token}",
"GData-Version" => "2",
"X-GData-Key" => "key=#{dev_key}",
"Content-Type" => "application/atom+xml; charset=UTF-8"
}
upload_response = RestClient.post("https://gdata.youtube.com/action/GetUploadToken", upload_metadata_xml, headers)
xml_response = Nokogiri::XML.parse(upload_response)
upload_url = xml_response.xpath("//url").first.children.inner_text()
upload_token = xml_response.xpath("//token").first.children.inner_text()
我在发布数据的行上不断收到错误。
upload_response = RestClient.post("https://gdata.youtube.com/action/GetUploadToken", upload_metadata_xml, headers)
错误说401 Unauthorized!
我在Google Chrome中使用Postman
扩展程序尝试了同样的操作。引发同样的错误。
我确信访问凭据是正确的。我多次检查过。
有人曾经实现过这个吗?或者,如果用户不必登录谷歌,还有其他方法可以实现这一目标。
编辑1
基于SF上的这个问题:Google Calendar API v3 hardcoded credentials
我尝试使用authorization
单独使用Google::APIClient::InstalledAppFlow
来尝试相同的脚本,但它仍会抛出相同的401 Unauthorized!
。
答案 0 :(得分:0)
除非您将在Web服务器中运行它,否则它将是已安装的应用程序。 否则,您将选择正确识别重定向地址的Web应用程序。
您可以通过从OAuth2 Playground获取刷新令牌并在youtube对象中设置它来实现此目的。