Class: Appydave::Tools::YouTubeManager::Authorization

Inherits:
Object
  • Object
show all
Defined in:
lib/appydave/tools/youtube_manager/authorization.rb

Overview

Handle YouTube API Authorization

Constant Summary collapse

REDIRECT_URI =
'http://localhost:8080/'
CLIENT_SECRETS_PATH =
File.join(Dir.home, '.config', 'appydave-google-youtube.json')
CREDENTIALS_PATH =
File.join(Dir.home, '.credentials', 'ad_youtube.yaml')
SCOPE =
[
  'https://www.googleapis.com/auth/youtube.readonly',
  'https://www.googleapis.com/auth/youtube',
  'https://www.googleapis.com/auth/youtube.force-ssl',
  'https://www.googleapis.com/auth/youtubepartner',
  'https://www.googleapis.com/auth/youtubepartner-channel-audit'
].freeze

Class Method Summary collapse

Class Method Details

.authorizeObject



20
21
22
23
24
25
26
27
28
29
30
# File 'lib/appydave/tools/youtube_manager/authorization.rb', line 20

def self.authorize
  FileUtils.mkdir_p(File.dirname(CREDENTIALS_PATH))

  client_id = Google::Auth::ClientId.from_file(CLIENT_SECRETS_PATH)
  token_store = Google::Auth::Stores::FileTokenStore.new(file: CREDENTIALS_PATH)
  authorizer = Google::Auth::UserAuthorizer.new(client_id, SCOPE, token_store)
  user_id = 'default'
  credentials = authorizer.get_credentials(user_id)
  credentials = wait_for_authorization(authorizer, user_id) if credentials.nil?
  credentials
end

.wait_for_authorization(authorizer, user_id) ⇒ Object



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/appydave/tools/youtube_manager/authorization.rb', line 32

def self.wait_for_authorization(authorizer, user_id)
  url = authorizer.get_authorization_url(base_url: REDIRECT_URI)
  puts 'Open the following URL in your browser and authorize the application:'
  puts url

  server = WEBrick::HTTPServer.new(Port: 8080, AccessLog: [], Logger: WEBrick::Log.new(nil, 0))
  trap('INT') { server.shutdown }

  server.mount_proc '/' do |req, res|
    auth_code = req.query['code']
    res.body = 'Authorization successful. You can close this window now.'
    server.shutdown
    authorizer.get_and_store_credentials_from_code(
      user_id: user_id, code: auth_code, base_url: REDIRECT_URI
    )
  end

  server.start
end