【发布时间】:2017-07-11 14:58:38
【问题描述】:
我在 Rails 上有一个应用程序 ruby,我正在尝试在 google 中授权我的应用程序,但我总是收到 uninitialized constant
我尝试使用Google::Auth::Stores::TokenStore 和Google::APIClient::InstalledAppFlow
我关注了这些样本 https://github.com/google/google-auth-library-ruby#example-command-line 和 https://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::TokenStore和uninitialized 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