授权Google Oauth2

时间:2017-07-11 14:58:39

标签: ruby-on-rails ruby google-maps-api-3

我有一个应用程序ruby on rails并且我试图在google中授权我的应用程序,但我总是得到uninitialized constant

我尝试使用Google::Auth::Stores::TokenStoreGoogle::APIClient::InstalledAppFlow

我遵循了这些示例https://github.com/google/google-auth-library-ruby#example-command-linehttps://developers.google.com/youtube/v3/code_samples/ruby?hl=pt-br#upload_a_video

一次使用googleauth和其他google-api-client

在我的Gemfile中

gem 'google-api-client', '0.8.2', require: 'google/api_client' gem 'googleauth'

在我的代码中

使用第一个例子

def authorization
    client_id = Google::Auth::ClientId.from_file './client_secrets.json'
    scope = ['SCOPE']
    token_store = Google::Auth::Stores::FileTokenStore.new(file: './tokens.yaml')
    authorizer = Google::Auth::UserAuthorizer.new(client_id, scope, token_store)
    credentials = authorizer.get_credentials(user_id)
    if credentials.nil?
        url = authorizer.get_authorization_url(base_url: OOB_URI )
        puts "Open #{url} in your browser and enter the resulting code:"
        code = gets
        credentials = authorizer.get_and_store_credentials_from_code(
        user_id: user_id, code: code, base_url: OOB_URI)
    end
end

使用第二个例子

def authorization
    file_storage = Google::APIClient::FileStore.new(oauth2)
    if file_storage.authorization.nil?
        client_secrets = Google::APIClient::ClientSecrets.load
        flow = Google::APIClient::InstalledAppFlow.new(
        client_id: client_secrets.client_id,
        client_secret: client_secrets.client_secret,
        scope: [YOUTUBE_UPLOAD_SCOPE]
        )
        client.authorization = flow.authorize(file_storage)
    else
        client.authorization = file_storage.authorization
    end
end

1 个答案:

答案 0 :(得分:1)

我使用第二种情况找到了解决方案。当我在Gemfile中添加gem gem 'google-api-client', '>0.7', require: 'google/api_client'时,我认为一切都已完成,但Google::APIClient::FileStorageGoogle::APIClient::InstalledAppFlow需要更多文件,因此在文件中你将使用这些人添加这些行< / p>

require 'google/api_client/auth/file_storage'
require 'google/api_client/auth/installed_app'