我正在使用谷歌云端硬盘API与我的Rails应用程序。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"
}
它在我的控制器中被称为
@session = GoogleDrive::Session.from_service_account_key("client_secret.json")
使用此配置没有问题,我成功地使用了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"
}"
然后在控制器中以这种方式调用它
@session = GoogleDrive::Session.from_service_account_key(ENV['CLIENT_SECRET'])
或者这样做
@session = GoogleDrive::Session.from_service_account_key(JSON.parse(ENV['CLIENT_SECRET']))
但是这两种方法都不起作用。所以我的问题是:"是否可能将JSON文件存储在ENV变量中?"
SURVEY_OBJ={"IncludeSurveys" : ["abc","wxy","z"]}
,然后通过使用let myObj = JSON.parse(process.env.SURVEY_OBJ)
来解析该环境变量。echo myObj.IncludeSurveys[1]
应该输出wxy
。因此,无需转义引号或删除空格。 - w. Patrick Gale