How to use Google API credentials json on Heroku?

后端 未结 10 1403
深忆病人
深忆病人 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:05

    If anyone is still looking for this, I've just managed to get this working for Google Cloud Storage by storing the JSON directly in an env variable (no extra buildpacks).

    You'll need to place the json credentials data into your env vars and install google-auth

    Then, parse the json and pass google credentials to the storage client:

    from google.cloud import storage
    from google.oauth2 import service_account
    
    # the json credentials stored as env variable
    json_str = os.environ.get('GOOGLE_APPLICATION_CREDENTIALS')
    # project name
    gcp_project = os.environ.get('GCP_PROJECT') 
    
    # generate json - if there are errors here remove newlines in .env
    json_data = json.loads(json_str)
    # the private_key needs to replace \n parsed as string literal with escaped newlines
    json_data['private_key'] = json_data['private_key'].replace('\\n', '\n')
    
    # use service_account to generate credentials object
    credentials = service_account.Credentials.from_service_account_info(
        json_data)
    
    # pass credentials AND project name to new client object (did not work wihout project name)
    storage_client = storage.Client(
        project=gcp_project, credentials=credentials)
    

    Hope this helps!

    EDIT: Clarified that this was for Google Cloud Storage. These classes will differ for other services, but from the looks of other docs the different Google Client classes should allow the passing of credentials objects.

    0 讨论(0)
  • 2020-12-03 06:08

    A more official Heroku documentation in this topic: https://elements.heroku.com/buildpacks/buyersight/heroku-google-application-credentials-buildpack

    I also used buyersight's buildpack and it was the only one, which worked for me

    0 讨论(0)
  • 2020-12-03 06:11

    I spent an entire day to find the solution because it's tricky. No matter your language, the solution will be the same.

    1 - Declare your env variables from in Heroku dashboard like :

    The GOOGLE_CREDENTIALS variable is the content of service account credential JSON file as is. The GOOGLE_APPLICATION_CREDENTIALS env variable in the string "google-credentials.json"

    2 - Once variables are declared, add the builpack from command line :

    $ heroku buildpacks:add https://github.com/elishaterada/heroku-google-application-credentials-buildpack
    

    3 - Make a push. Update a tiny thing and push.

    4 - The buildpack will automatically generate a google-credentials.json and fill it with the content of the google credentials content.

    If you failed at something, it will not work. Check the content of the google-credentials.json with the Heroku bash.

    0 讨论(0)
  • 2020-12-03 06:11

    You can use the Heroku Platform API to update Heroku env vars from within your app.

    0 讨论(0)
  • It seems that those buildpacks where you can upload the credentials.json file are not working as expected. I finally managed with Lepinsk's buildpack (https://github.com/lepinsk/heroku-google-cloud-buildpack.git), which requires all keys and values to be set as config vars in Heroku. It does do the job though, so lots of thanks for that!

    0 讨论(0)
  • 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
    
    0 讨论(0)
提交回复
热议问题