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

后端 未结 2 1060
栀梦
栀梦 2021-02-14 23:12

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\",
         


        
相关标签:
2条回答
  • 2021-02-14 23:42

    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.

    0 讨论(0)
  • 2021-02-14 23:50

    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

    0 讨论(0)
提交回复
热议问题