小编Que*_* P.的帖子

是否可以使用 dotenv 将 JSON 文件存储到 ENV 变量中?

我在 Rails 应用程序中使用 Google Drive API。API 工作正常。我有以下 client_secret.json 文件:

{
  "type": "service_account",
  "project_id": "gobirdie-landing-page",
  "private_key_id": "xxxxx",
  "private_key": "-----BEGIN PRIVATE KEY----- xxxxx -----END PRIVATE KEY-----\n",
  "client_email": "xxxxxxx@gobirdie-landing-page.iam.gserviceaccount.com",
  "client_id": "xxxxxxxxx",
  "auth_uri": "xxxxxx",
  "token_uri": "xxxxxxx": "xxxxxxxx": "xxxxxxxxx"
}
Run Code Online (Sandbox Code Playgroud)

在我的控制器中调用

@session = GoogleDrive::Session.from_service_account_key("client_secret.json")

Run Code Online (Sandbox Code Playgroud)

有了这个配置没问题,我设法使用 API。但是,我想将我的 JSON 存储在 .env 文件中,例如:

CLIENT_SECRET = "{
  "type": "service_account",
  "project_id": "gobirdie-landing-page",
  "private_key_id": "xxxxx",
  "private_key": "-----BEGIN PRIVATE KEY----- xxxxx -----END PRIVATE KEY-----\n",
  "client_email": "xxxxxxx@gobirdie-landing-page.iam.gserviceaccount.com",
  "client_id": "xxxxxxxxx",
  "auth_uri": "xxxxxx",
  "token_uri": "xxxxxxx": "xxxxxxxx": "xxxxxxxxx"
}" 
Run Code Online (Sandbox Code Playgroud)

并以这种方式在控制器中调用

@session = GoogleDrive::Session.from_service_account_key(ENV['CLIENT_SECRET'])
Run Code Online (Sandbox Code Playgroud)

或者以这种方式

@session = …
Run Code Online (Sandbox Code Playgroud)

json ruby-on-rails ruby-dotenv

17
推荐指数
2
解决办法
2万
查看次数

标签 统计

json ×1

ruby-dotenv ×1

ruby-on-rails ×1