如何通过红宝石通过服务帐户插入视频YouTube YouTube API V3

丹加尔

我的目标是让我的应用程序的用户通过我的服务器上传到我的youtube帐户。我有一个开发人员帐户,已启用youtube数据api和服务帐户客户端设置,由于某种原因,我未被授权。我通过网络进行挖掘,试图找出原因,并发现了很多东西。我尝试使用项目客户端ID和范围在管理员安全设置中授予权限。许多人说这个错误是由于我的帐户没有关联的youtube帐户引起的。但我不知道如何将它们关联起来。

这是我的upload_video.rb脚本:

require 'rubygems'
require 'google/api_client'
require 'google/api_client/client_secrets'
require 'google/api_client/auth/file_storage'
require 'google/api_client/auth/installed_app'
require 'certified'

DEVELOPER_KEY = "MY-DEVELOPER-KEY"
YOUTUBE_UPLOAD_SCOPE = 'https://www.googleapis.com/auth/youtube.upload'
YOUTUBE_API_SERVICE_NAME = 'youtube'
YOUTUBE_API_VERSION = 'v3'

def get_authenticated_service
  puts @PROGRAM_NAME
  client = Google::APIClient.new(
    :application_name => $PROGRAM_NAME,
    :application_version => '1.0.0'
)

key = Google::APIClient::KeyUtils.load_from_pkcs12('oauth2service.p12', 'notasecret')
  auth_client = Signet::OAuth2::Client.new(
  :token_credential_uri => 'https://accounts.google.com/o/oauth2/token',
  :audience => 'https://accounts.google.com/o/oauth2/token',
  :scope => YOUTUBE_UPLOAD_SCOPE,
  :issuer => 'SERVICE ACCOUNT EMAIL ADDRESS',
  :signing_key => key)
 auth_client.fetch_access_token!
 client.authorization = auth_client
 youtube = client.discovered_api(YOUTUBE_API_SERVICE_NAME, YOUTUBE_API_VERSION)
 return client, youtube
end

def upload2youtube file, title, description, category_id, keywords, privacy_status

  client, youtube = get_authenticated_service
  begin
    body = {
  :snippet => {
    :title => title,
    :description => description,
    :tags => keywords.split(','),
    :categoryId => category_id,
  },
  :status => {
    :privacyStatus => privacy_status
  }
}

videos_insert_response = client.execute!(
  :api_method => youtube.videos.insert,
  :body_object => body,
  :media => Google::APIClient::UploadIO.new(file, 'video/*'),
  :parameters => {
    :uploadType => 'resumable',
    :part => body.keys.join(',')
  }
 )

 videos_insert_response.resumable_upload.send_all(client)

 puts "Video id '#{videos_insert_response.data.id}' was successfully uploaded."
 rescue Google::APIClient::TransmissionError => e
 puts e.result.body
 end
end

这就是我从另一个脚本运行它的方式:

require 'google/api_client'
require_relative 'upload_video'

$PROGRAM_NAME = 'MY-PROJECT-NAME'
upload2youtube File.open("somevideo.avi"), "title", "description", 'categoryid', 'tag1,tag2,tag3', 'unlisted'

结果如下:

{
 "error": {
  "errors": [
   {
    "domain": "youtube.header",
    "reason": "youtubeSignupRequired",
    "message": "Unauthorized",
    "locationType": "header",
    "location": "Authorization"
   }
  ],
  "code": 401,
  "message": "Unauthorized"
 }
}

我究竟做错了什么

丹加尔
#!/usr/bin/ruby

require 'rubygems'
require 'google/api_client'
require 'google/api_client/client_secrets'
require 'google/api_client/auth/file_storage'
require 'google/api_client/auth/installed_app'
require 'certified'


class Youtube_Helper

  @@client_email = '' #email id from service account (that really long email address...)
  @@youtube_email = '' #email associated with youtube account
  @@p12_file_path = '' #path to the file downloaded from the service account (Generate new p12 key button)
  @@p12_password = '' # password to the file usually 'notasecret'
  @@api_key = nil # The API key for non authenticated things like lists
  YOUTUBE_UPLOAD_SCOPE = 'https://www.googleapis.com/auth/youtube.upload'
  YOUTUBE_API_SERVICE_NAME = 'youtube'
  YOUTUBE_API_VERSION = 'v3'
  @@client = nil
  @@youtube = nil
  FILE_POSTFIX = '-oauth2.json'

  def initialize(client_email, youtube_email, p12_file_path, p12_password, api_key)
    @@client_email=client_email
    @@youtube_email=youtube_email
    @@p12_file_path=p12_file_path
    @@p12_password=p12_password
    @@api_key = api_key
  end

  def get_authenticated_service


    credentialsFile = $0 + FILE_POSTFIX

    needtoauthenticate = false

    @api_client = Google::APIClient.new(
      :application_name => $PROGRAM_NAME,
      :application_version => '1.0.0'
    )

    key = Google::APIClient::KeyUtils.load_from_pkcs12(@@p12_file_path, @@p12_password)
    @auth_client = Signet::OAuth2::Client.new(
        :token_credential_uri => 'https://accounts.google.com/o/oauth2/token',
        :audience => 'https://accounts.google.com/o/oauth2/token',
        :scope => YOUTUBE_UPLOAD_SCOPE,
        :issuer => @@client_email,
        :person => @@youtube_email,
        :signing_key => key)


    if File.exist? credentialsFile
      puts 'credential file exists'
      puts credentialsFile.to_s
      File.open(credentialsFile, 'r') do |file|
        credentials = JSON.load(file)
        if !credentials.nil?
          puts 'get credentials from file'
          @auth_client.access_token = credentials['access_token']
          @auth_client.client_id = credentials['client_id']
          @auth_client.client_secret = credentials['client_secret']
          @auth_client.refresh_token = credentials['refresh_token']
          @auth_client.expires_in = (Time.parse(credentials['token_expiry']) - Time.now).ceil
          @api_client.authorization = @auth_client
          if @auth_client.expired?
            puts 'authorization expired'
            needtoauthenticate = true
          end
        else
          needtoauthenticate = true
        end
      end
    else
      needtoauthenticate = true
    end

    if needtoauthenticate
      @auth_client.fetch_access_token!
      @api_client.authorization = @auth_client
      save(credentialsFile)
    end

    youtube = @api_client.discovered_api(YOUTUBE_API_SERVICE_NAME, YOUTUBE_API_VERSION)
    @@client = @api_client
    @@youtube = youtube
    return @api_client, youtube
  end

  def save(credentialsFile)
    File.open(credentialsFile, 'w', 0600) do |file|
      json = JSON.dump({
        :access_token => @auth_client.access_token,
        :client_id => @auth_client.client_id,
        :client_secret => @auth_client.client_secret,
        :refresh_token => @auth_client.refresh_token,
        :token_expiry => @auth_client.expires_at
      })
      file.write(json)
    end
  end

  def upload2youtube file, title, description, category_id, keywords, privacy_status
    puts 'begin'
    begin
      body = {
        :snippet => {
          :title => title,
          :description => description,
          :tags => keywords.split(','),
          :categoryId => category_id,
        },
        :status => {
          :privacyStatus => privacy_status
        }
      }
      puts body.keys.join(',')

      # Call the API's videos.insert method to create and upload the video.
      videos_insert_response = @@client.execute!(
        :api_method => @@youtube.videos.insert,
        :body_object => body,
        :media => Google::APIClient::UploadIO.new(file, 'video/*'),
        :parameters => {
          'uploadType' => 'multipart',
          :part => body.keys.join(',')
        }
      )

      puts'inserted'

      puts "'#{videos_insert_response.data.snippet.title}' (video id: #{videos_insert_response.data.id}) was successfully uploaded."

    rescue Google::APIClient::TransmissionError => e
      puts e.result.body
    end

    return videos_insert_response.data.id #video id

  end

  def upload_thumbnail  video_id, thumbnail_file, thumbnail_size
    puts 'uploading thumbnail'
    begin
      thumbnail_upload_response = @@client.execute({ :api_method => @@youtube.thumbnails.set,
                            :parameters => { :videoId => video_id,
                                             'uploadType' => 'media',
                                             :onBehalfOfContentOwner => @@youtube_email},
                            :media => thumbnail_file,
                            :headers => { 'Content-Length' => thumbnail_size.to_s,
                                          'Content-Type' => 'image/jpg' }
                            })
    rescue Google::APIClient::TransmissionError => e
        puts e.result.body 
    end 
  end

  def get_video_statistics video_id
    client = Google::APIClient.new(:key => @@api_key,
                                    :application_name => $PROGRAM_NAME,
                                    :application_version => '1.0.0',
                                    :authorization => nil)
    youtube = client.discovered_api(YOUTUBE_API_SERVICE_NAME, YOUTUBE_API_VERSION)
    stats_response = client.execute!( :api_method => youtube.videos.list,
                    :parameters => {:part => 'statistics', :id => video_id }
                    )
    return stats_response
  end
end

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

Youtube v3 Api 通过ID获取视频

来自分类Dev

PHP-如何通过Youtube API v3更改youtube视频的隐私状态?

来自分类Dev

如何通过Youtube API v3 YouTube.PlaylistItems.List请求检索视频的时长?

来自分类Dev

PHP-如何通过Youtube API v3更改youtube视频的隐私状态?

来自分类Dev

Youtube v3 API视频语言

来自分类Dev

如何使用YouTube API V3?

来自分类Dev

使用Youtube V3 API的YouTube相关视频

来自分类Dev

尝试通过YouTube API V3访问正确的YouTube帐户

来自分类Dev

Youtube API V3和Etag

来自分类Dev

Youtube API v3 .NET集成

来自分类Dev

页面令牌youtube api v3

来自分类Dev

YouTube API v3获取评论

来自分类Dev

youtube数据API v3搜索

来自分类Dev

Youtube API V3和Etag

来自分类Dev

YouTube iframe API v3限制

来自分类Dev

Gwt YouTube数据API v3

来自分类Dev

Youtube API v3获得评论

来自分类Dev

Youtube api v3添加评论

来自分类Dev

解析 youtube 数据 v3 api

来自分类Dev

如何获取视频Youtube的默认语言(API v3)

来自分类Dev

使用YouTube API v3获取视频的完整说明

来自分类Dev

在PHP中转换YouTube Api v3视频时长

来自分类Dev

使用YouTube数据API v3的视频元数据

来自分类Dev

YouTube API v3获取视频类别

来自分类Dev

使用YouTube API V3获取channelId的视频失败

来自分类Dev

YouTube API v3未检索频道的视频

来自分类Dev

使用YouTube数据API v3的视频元数据

来自分类Dev

YouTube API v3:视频回复的contentDetails.contentRating

来自分类Dev

使用YouTube API v3 PHP删除视频