如何通过GoogleAuth进行身份验证,而不在文件中存储凭证。

问题描述 投票:0回答:1

当我在学习如何实现一个通过API与Google日历交互的功能时,我看到了一个教程,该教程使用以下方法来验证使用 credentials.json 文件。

CREDENTIALS_PATH = "credentials.json".freeze
client_id = Google::Auth::ClientId.from_file CREDENTIALS_PATH

根据文件记载,我们也可以使用 from_hash. 问题是,即使把 一模一样 来自 credentials.json 文件并将其转化为哈希文件是行不通的。请看下面的内容。

[6] pry(main)> creds
=> {:installed=>
  {:client_id=>"[redacted]",
   :project_id=>"myapp-232803",
   :auth_uri=>"https://accounts.google.com/o/oauth2/auth",
   :token_uri=>"https://oauth2.googleapis.com/token",
   :auth_provider_x509_cert_url=>"https://www.googleapis.com/oauth2/v1/certs",
   :client_secret=>"[redacted]",
   :redirect_uris=>["urn:ietf:wg:oauth:2.0:oob", "http://localhost"]}}
[7] pry(main)> client_id = Google::Auth::ClientId.from_hash(creds)
RuntimeError: Expected top level property 'installed' or 'web' to be present.
from /Library/Ruby/Gems/2.6.0/gems/googleauth-0.12.0/lib/googleauth/client_id.rb:99:in `from_hash'

正如你所看到的,它在抱怨 "安装 "或 "网络 "这些顶级属性需要存在,但根据哈希值,它是存在的。

任何帮助将非常感激。只是想避免将任何敏感信息存储在文件中,而是在环境变量中使用,因为我将部署应用程序。

ruby-on-rails ruby google-authentication
1个回答
0
投票

找到了问题所在。

# /Library/Ruby/Gems/2.6.0/gems/googleauth-0.12.0/lib/googleauth/client_id.rb
def self.from_hash config
    raise "Hash can not be nil." if config.nil?
    raw_detail = config[INSTALLED_APP] || config[WEB_APP]
    raise MISSING_TOP_LEVEL_ELEMENT_ERROR if raw_detail.nil?
    ClientId.new raw_detail[CLIENT_ID], raw_detail[CLIENT_SECRET]
end

我只是简单地把哈希值从 "key":"value""key" => "value" 一切都能完美地工作。然而,不知道为什么要这样做,但这确实是个好办法。

© www.soinside.com 2019 - 2024. All rights reserved.