“需要登录”尝试使用YoutubeV3 API创建实况广播时出错 [英] "Login Required" error when trying to create live broadcast using YoutubeV3 API

查看:227
本文介绍了“需要登录”尝试使用YoutubeV3 API创建实况广播时出错的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用 google-api-ruby-client

  YT = Google :: Apis :: YoutubeV3 
client = YT :: YouTubeService.new
client.key ='my-api-key-here'
metadata = {
snippet:{
title:'test',
scheduled_start_time:'2018-02- 23T14:50:00.000Z'
},
status:{
privacy_status:'public'
}
}
part ='snippet'
client.insert_live_broadcast(part,metadata,{})

当我执行这段代码时, 错误 - #< Google :: Apis :: AuthorizationError:Unauthorized>

  [5] pry(main)> CreateYoutubeBroadcast.execute 
发送HTTP后https://www.googleapis.com/youtube/v3/liveBroadcasts?key=my-api-key&part=snippet
401
#< HTTP :: Message:0x007f8d5356fdc8 @http_header =#< HTTP :: Message :: Headers:0x007f8d5356fda0 @ http_version =1.1,@ body_size = 0,@ chunked = false,@ request_method =POST,@request_uri =#< Addressable :: URI:0x3fc6a61124c8 URI:https://www.googleapis.com/youtube/v3/liveBroadcasts?key = my-api-key& part = snippet> ;, @ request_query = nil,@ request_absolute_uri = nil,@ status_code = 401,@ reason_phrase =Unauthorized,@ body_type = nil,@ body_charset = nil,@ body_date = nil,@body_encoding =#< Encoding:UTF-8>,is_request = false,@header_item = [[Vary ,Origin,[Vary,X-Origin],[WWW-Authenticate,Bearer realm = \https://accounts.google.com/\],[内容类型,application / json; charset = UTF-8],[内容编码,gzip],[日期,星期四,2018年2月15日12:59:45 GMT], [过期,星期四,2018年2月15日12:59:45 GMT],[缓存控制, ,X-Frame-Options,SAMEORIGIN],[X-XSS-Protection, 1; mode = block],[Server,GSE],[Alt-Svc,hq = \:443 \; MA = 2592000; QUIC = 51303431; QUIC = 51303339; QUIC = 51303338; QUIC = 51303337; QUIC = 51303335,QUIC = \ :443\; MA = 2592000; v = \41,39,38,37,35 \],[Transfer-Encoding,chunked]],@ dumped = false>,@peer_cert =#< OpenSSL :: X509: :证书:subject =#<< OpenSSL :: X509 :: Name:0x007f8d5247d290>,issuer =#< OpenSSL :: X509 :: Name:0x007f8d5247d268> ;, serial =#< OpenSSL :: BN:0x007f8d5247d240> ;, not_before = 2018-01-30 08:56:10 UTC,not_after = 2018-04-24 08:30:00 UTC>,@http_body =#< HTTP :: Message :: Body:0x007f8d5356fd28 @body ={\\\
\error \:{\\\
\errors \:[\\\
{\ \\ n \domain \:\global \,\\\
\理由:\required \,\\\
\message \:\需要登录\,\\\
\locationType \:\header \ ,\\ \location \:\Authorization \\\
} \\\
],\\\\\\\\\\\\\代码\:401,\\\ \message \\ :\需要登录\\\\
} \\\
} \ n,@ size = 0,@ positions = nil,@ chunk_size = nil> ;, @ previous = nil>
Caught error未经授权
错误 - #< Google :: Apis :: AuthorizationError:Unauthorized>

认证失败后重试
Google :: Apis :: AuthorizationError:未授权

我已经完成



任何想法为什么会发生此错误?谢谢

解决方案

似乎通过API密钥创建预定广播是不可能的,我使用Google OAuth 2.0策略来完成此操作。

 #lib / youtube_api / o_auth_client.rb 

需要'google / api_client / auth / storage'
需要'google / api_client / auth / storages / file_store'
需要'google / api_client / auth / installed_app'

模块YoutubeApi
类OAuthClient
YOUTUBE_SCOPE ='https://www.googleapis.com/auth/youtube'
CREDENTIALS_FILE = Rails.root.join('tmp','google_api_credentials.json')

def初始化
@credentials_storage = :: Google :: APIClient :: Storage.new(
:: Google :: APIClient :: FileStore.new(CREDENTIALS_FILE)

结束

def授权
@ credentials_storage.authorize ||开始
installed_app_flow = :: Google :: APIClient :: InstalledAppFlow.new(
client_id:Rails.application.secrets.youtube_client_id,
client_secret:Rails.application.secrets.youtube_client_secret,
范围:[YOUTUBE_SCOPE]

installed_app_flow.authorize(@credentials_storage)
结束
结束
结束
结束

和我的create_broadcast服务对象

 需要'服务'
需要'youtube_api'
需要'google / apis / youtube_v3'


类CreateBroadcast
包含服务

YT = Google :: Apis :: YoutubeV3
BROADCAST_PRIVACY ='public'

def初始化(title,description,start_time)
@client = YT :: YouTubeService.new
@ client.authorization = YoutubeApi.authorize_via_google_oauth_2
@title = title
@description = description
@start_ time = start_time
end

def execute
@ client.insert_live_broadcast(part,metadata,{})
结束

私有

def part
'id,snippet,contentDetails,status'
end

def metadata
{
snippet:{
title:@title,
description:@description,
scheduled_start_time:@start_time,
},
status:{
privacy_status:BROADCAST_PRIVACY
}
}
结束
结束

lib / youtube_api.rb 我有

 #frozen_string_literal:true 

需要'youtube_api / o_auth_client'

模块YoutubeApi
def self.authorize_via_google_oauth_2
oauth_client = OAuthClient.new
oauth_client.authorize
end
结束


I am trying to create a new broadcast using google-api-ruby-client

YT = Google::Apis::YoutubeV3
client = YT::YouTubeService.new
client.key = 'my-api-key-here'
metadata = {
  snippet: {
    title: 'test',
    scheduled_start_time: '2018-02-23T14:50:00.000Z'
  },
  status: {
    privacy_status: 'public'
  }
}
part = 'snippet'
client.insert_live_broadcast(part, metadata, {})

When I execute this code I am getting Error -# <Google::Apis::AuthorizationError: Unauthorized>

[5] pry(main)> CreateYoutubeBroadcast.execute
Sending HTTP post https://www.googleapis.com/youtube/v3/liveBroadcasts?key=my-api-key&part=snippet
401
#<HTTP::Message:0x007f8d5356fdc8 @http_header=#<HTTP::Message::Headers:0x007f8d5356fda0 @http_version="1.1", @body_size=0, @chunked=false, @request_method="POST", @request_uri=#<Addressable::URI:0x3fc6a61124c8 URI:https://www.googleapis.com/youtube/v3/liveBroadcasts?key=my-api-key&part=snippet>, @request_query=nil, @request_absolute_uri=nil, @status_code=401, @reason_phrase="Unauthorized", @body_type=nil, @body_charset=nil, @body_date=nil, @body_encoding=#<Encoding:UTF-8>, @is_request=false, @header_item=[["Vary", "Origin"], ["Vary", "X-Origin"], ["WWW-Authenticate", "Bearer realm=\"https://accounts.google.com/\""], ["Content-Type", "application/json; charset=UTF-8"], ["Content-Encoding", "gzip"], ["Date", "Thu, 15 Feb 2018 12:59:45 GMT"], ["Expires", "Thu, 15 Feb 2018 12:59:45 GMT"], ["Cache-Control", "private, max-age=0"], ["X-Content-Type-Options", "nosniff"], ["X-Frame-Options", "SAMEORIGIN"], ["X-XSS-Protection", "1; mode=block"], ["Server", "GSE"], ["Alt-Svc", "hq=\":443\"; ma=2592000; quic=51303431; quic=51303339; quic=51303338; quic=51303337; quic=51303335,quic=\":443\"; ma=2592000; v=\"41,39,38,37,35\""], ["Transfer-Encoding", "chunked"]], @dumped=false>, @peer_cert=#<OpenSSL::X509::Certificate: subject=#<OpenSSL::X509::Name:0x007f8d5247d290>, issuer=#<OpenSSL::X509::Name:0x007f8d5247d268>, serial=#<OpenSSL::BN:0x007f8d5247d240>, not_before=2018-01-30 08:56:10 UTC, not_after=2018-04-24 08:30:00 UTC>, @http_body=#<HTTP::Message::Body:0x007f8d5356fd28 @body="{\n \"error\": {\n  \"errors\": [\n   {\n    \"domain\": \"global\",\n    \"reason\": \"required\",\n    \"message\": \"Login Required\",\n    \"locationType\": \"header\",\n    \"location\": \"Authorization\"\n   }\n  ],\n  \"code\": 401,\n  \"message\": \"Login Required\"\n }\n}\n", @size=0, @positions=nil, @chunk_size=nil>, @previous=nil>
Caught error Unauthorized
Error - #<Google::Apis::AuthorizationError: Unauthorized>

Retrying after authentication failure
Google::Apis::AuthorizationError: Unauthorized

I have done

Any idea why this error is occurring? Thank you

解决方案

It seems creating scheduled broadcasts is not possible via API keys, I used google OAuth 2.0 strategy to get this done.

# lib/youtube_api/o_auth_client.rb

require 'google/api_client/auth/storage'
require 'google/api_client/auth/storages/file_store'
require 'google/api_client/auth/installed_app'

module YoutubeApi
  class OAuthClient
    YOUTUBE_SCOPE = 'https://www.googleapis.com/auth/youtube'
    CREDENTIALS_FILE = Rails.root.join('tmp', 'google_api_credentials.json')

    def initialize
      @credentials_storage = ::Google::APIClient::Storage.new(
        ::Google::APIClient::FileStore.new(CREDENTIALS_FILE)
      )
    end

    def authorize
      @credentials_storage.authorize || begin
        installed_app_flow = ::Google::APIClient::InstalledAppFlow.new(
          client_id:     Rails.application.secrets.youtube_client_id,
          client_secret: Rails.application.secrets.youtube_client_secret,
          scope:         [YOUTUBE_SCOPE]
        )
        installed_app_flow.authorize(@credentials_storage)
      end
    end
  end
end

and my create_broadcast service object

require 'service'
require 'youtube_api'
require 'google/apis/youtube_v3'


class CreateBroadcast
  include Service

  YT = Google::Apis::YoutubeV3
  BROADCAST_PRIVACY = 'public'

  def initialize(title, description, start_time)
    @client = YT::YouTubeService.new
    @client.authorization = YoutubeApi.authorize_via_google_oauth_2
    @title = title
    @description = description
    @start_time = start_time
  end

  def execute
    @client.insert_live_broadcast(part, metadata, {})
  end

  private

    def part
      'id,snippet,contentDetails,status'
    end

    def metadata
      {
        snippet: {
          title: @title,
          description: @description,
          scheduled_start_time: @start_time,
        },
        status: {
          privacy_status: BROADCAST_PRIVACY
        }
      }
    end
end

and in lib/youtube_api.rb I have

# frozen_string_literal: true

require 'youtube_api/o_auth_client'

module YoutubeApi
  def self.authorize_via_google_oauth_2
    oauth_client = OAuthClient.new
    oauth_client.authorize
  end
end

这篇关于“需要登录”尝试使用YoutubeV3 API创建实况广播时出错的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆