Que*_* P. 17 json ruby-on-rails ruby-dotenv
我在 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 = GoogleDrive::Session.from_service_account_key(JSON.parse(ENV['CLIENT_SECRET']))
Run Code Online (Sandbox Code Playgroud)
但是这两种方法都不起作用。所以我的问题是:“是否可以将 JSON 文件存储在 ENV 变量中?”
PGi*_*ill 13
将 JSON 对象转换为字符串并将其存储在 ENV 中
您可以使用JSON.dump将 JSON 对象转换为字符串
然后在你的控制器中 JSON.parse(ENV['CLIENT_SECRET'])
或者
您可以创建一个google_session.rb内部initializers文件夹
$google_session = GoogleDrive::Session.from_service_account_key(
# config goes here
)
Run Code Online (Sandbox Code Playgroud)
在您的控制器中,您将可以访问$google_session全局变量
您可以将项目放在一行上,例如:
VITE_FIREBASE={"apiKey":"slslsls","slsls":"lelsls"}
Run Code Online (Sandbox Code Playgroud)
您只需确保您的密钥中包含引号即可。
并像这样获取它们(SvelteKit 示例):
VITE_FIREBASE={"apiKey":"slslsls","slsls":"lelsls"}
Run Code Online (Sandbox Code Playgroud)
J
| 归档时间: |
|
| 查看次数: |
17726 次 |
| 最近记录: |