【问题标题】:Authorize Google Oauth2授权 Google Oauth2
【发布时间】:2017-07-11 14:58:38
【问题描述】:

我在 Rails 上有一个应用程序 ruby​​,我正在尝试在 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

【问题讨论】:

  • 完整的错误信息是什么?它发生在哪条线上?两个身份验证流程是否相同?
  • 错误是uninitialized constant Google::Auth::Stores::TokenStoreuninitialized constant Google::APIClient::InstalledAppFlow 当我调用这些类时出现错误,因为它没有找到它,但我安装了gem。
  • 您在定义authorization 的文件/控制器中是否需要这些?
  • 我找到了解决方案。我需要在 Gemfile 中添加 gem,但在我的服务中我需要 'google/api_client/auth/file_storage' and 'google/api_client/auth/installed_app'´

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


【解决方案1】:

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

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

【讨论】:

    猜你喜欢
    • 2016-11-18
    • 2019-03-26
    • 2012-06-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-08-21
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多