简体   繁体   中英

Is it possible to store a JSON file to an ENV variable with dotenv?

I am using Google Drive API with my Rails application. The API is working fine. I have the following client_secret.json file:

{
  "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"
}

which is called in my controller

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

With this configuration no problem, I manage to use the API. However, I would like to store my JSON in the .env file like:

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"
}" 

And call it in the controller in this way

@session = GoogleDrive::Session.from_service_account_key(ENV['CLIENT_SECRET'])

Or in this way

@session = GoogleDrive::Session.from_service_account_key(JSON.parse(ENV['CLIENT_SECRET']))

But neither methods are working. So my question is : "Is it possible to store JSON file in an ENV variable ?"

Convert the JSON object to string and store it in the ENV

You can use JSON.dump to convert JSON object to string

and then in your controller JSON.parse(ENV['CLIENT_SECRET'])

Alternatively

you can create a google_session.rb inside initializers folder

$google_session = GoogleDrive::Session.from_service_account_key(
   # config goes here
)

and in your controller you will have access to $google_session global variable

Yes. It is possible to store json file in variable. However there is one small change needed :

\\\"type\\\": \\\"service_account\\\",

Do this for every double quote inside the curly braces of json.

I have tried to make the change as you suggest. My .env file now looks like

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\ \ \": \ \ \"xxxx@gobirdie-landing-page.iam.gserviceaccount.com\ \ \",
  \ \ \"client_id\ \ \": \ \ \"xxxx\ \ \",
  \ \ \"auth_uri\ \ \": \ \ \"https://accounts.google.com/o/oauth2/auth\ \ \",
  \ \ \"token_uri\ \ \": \ \ \"xxxxx\ \ \",
  \ \ \"auth_provider_x509_cert_url\ \ \": \ \ \"xxxx\ \ \",
  \ \ \"client_x509_cert_url\ \ \": \ \ \"https://www.googleapis.com/robot/v1/metadata/x509/landingpageserviceaccount2%40gobirdie-landing-page.iam.gserviceaccount.com\ \ \"
}

However when I load the page on localhost:3000, I still get the same error as before.

File name too long @ rb_sysopen - { "type": "service_account", ....... }

我想出的解决方案是对 json 对象进行基本编码并存储它,然后根据使用它的请求对其进行解码。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM