How to use Google API credentials json on Heroku?

后端 未结 10 1440
深忆病人
深忆病人 2020-12-03 05:36

I\'m making an app using Google Calendar API, and planning to build it on Heroku.
I have a problem about authentication. Usually I use credential json file for that, but

10条回答
  •  难免孤独
    2020-12-03 06:11

    I have done this like below:

    • Created two env variable
      1. CREDENTIALS - base64 encoded value of google api credential
      2. CONFIG_FILE_PATH - /app/.gcp/key.json (this file we will create it at run time. in heroku preinstall phase as below)

    Create a preinstall script.

    "heroku-prebuild": "bash preinstall.sh",
    

    And in preinstall.sh file, decode CREDENTIALS and create a config file and update it there.

    if [ "$CREDENTIALS" != "" ]; then
    
    
    echo "Detected credentials. Adding credentials" >&1
      echo "" >&1
    
      # Ensure we have an gcp folder
      if [ ! -d ./.gcp ]; then
        mkdir -p ./.gcp
        chmod 700 ./.gcp
      fi
    
      # Load the private key into a file.
      echo $GCP_CREDENTIALS | base64 --decode > ./.gcp/key.json
    
      # Change the permissions on the file to
      # be read-only for this user.
      chmod 400 ./.gcp/key.json
    fi
    

提交回复
热议问题