0

Youtube API v3を使用access_codeすると、サーバーで生成された動画をブラウザからアップロードできます。

RoRで実行されている Web サーバーには、次を使用して認証されるクライアント証明書があります。

Google::APIClient::JWTAsserter

access_codeに有効なを取得できますone hour

ブラウザベースのアップロードで使用するアプリケーションの開発者キーも持っています。

access_codeユーザーが自分のGoogleアカウントでログインする必要がないように、ブラウザと共有したい。

これを 1 つのスクリプトでシミュレートするためRestClientに、Ruby で使用しました。

これは、ドキュメントに基づいて試したサンプルです。

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!

PostmanGoogle Chromeの拡張機能で同じことを試しました。同じエラーをスローします。

アクセス資格情報が正しいと確信しています。私はそれを何度も確認しました。

誰かがこれを以前に実装しましたか?または、ユーザーが Google にログインしなくてもこれを実現する別の方法はありますか。

編集1

SF に関するこの質問に基づく: Google Calendar API v3 hardcoded credentials

authorizationalone を使用するように変更して同じスクリプトを試しましたが、Google::APIClient::InstalledAppFlowそれでも同じ がスローされ401 Unauthorized!ます。

4

1 に答える 1