brandonmp / gatsby-source-google-sheets

A GatsbyJS plugin that pulls nodes from rows in a Google Sheet.
90 stars 32 forks source link

environmental variables #32

Closed d1sc0 closed 5 years ago

d1sc0 commented 5 years ago

Is there a way to allow storing of google api credentials within .env variables?

krerkkiat commented 5 years ago

Maybe this is what you are looking for?

https://www.gatsbyjs.org/docs/environment-variables/#server-side-nodejs

d1sc0 commented 5 years ago

yes this is what I was trying to achieve but the docs for this plugin show configuration for this plugin as follows in gatsby-config.js

credentials: require('./path-to-credentials-file.json')

As the json file referenced holds multiple parameters I was unsure how I could make this work with .env variables which seem to be always referenced as individual options in all examples I could find. Sorry I might just be missing something obvious.

krerkkiat commented 5 years ago

It seems that require() will give back an object, so you can still use the method described in the link to load the information and then manually rebuild the object. It would be something like

// .env
SERVICE_ACCOUNT_TYPE=service_account
SERVICE_ACCOUNT_PROJECT_ID=id
SERVICE_ACCOUNT_PRIVATE_KEY_ID=key
SERVICE_ACCOUNT_PRIVATE_KEY=key
SERVICE_ACCOUNT_CLIENT_EMAIL=email
SERVICE_ACCOUNT_CLIENT_ID=id
SERVICE_ACCOUNT_AUTH_URI=uri
SERVICE_ACCOUNT_TOKEN_URI=uri
SERVICE_ACCOUNT_AUTH_PROVIDER_X509_CERT_URL=url
SERVICE_ACCOUNT_CLIENT_X509_CERT_URL=url
// gatsby-config.js
require("dotenv").config({
  path: `.env`,
})

var cred = {
  "type": process.env.SERVICE_ACCOUNT_TYPE,
  "project_id": process.env.SERVICE_ACCOUNT_PROJECT_ID,
  "private_key_id": process.env.SERVICE_ACCOUNT_PRIVATE_KEY_ID,
  "private_key": process.env.SERVICE_ACCOUNT_PRIVATE_KEY,
  "client_email": process.env.SERVICE_ACCOUNT_CLIENT_EMAIL,
  "client_id": process.env.SERVICE_ACCOUNT_CLIENT_ID,
  "auth_uri": process.env.SERVICE_ACCOUNT_AUTH_URI,
  "token_uri": process.env.SERVICE_ACCOUNT_TOKEN_URI,
  "auth_provider_x509_cert_url": process.env.SERVICE_ACCOUNT_AUTH_PROVIDER_X509_CERT_URL,
  "client_x509_cert_url": process.env.SERVICE_ACCOUNT_CLIENT_X509_CERT_URL
}

....
{
    resolve: 'gatsby-source-google-sheets',
    options: {
        spreadsheetId: 'get this from the sheet url',
        worksheetTitle: 'ie the name in the worksheet tab',
        credentials: cred
    }
},
d1sc0 commented 5 years ago

Thank you SOO Much. This has got it working well locally.

Problem I have now is when deployed to Netlify it fails to build - I've tried setting these values in the nelify UI environment variables but it does't seem to want to work

d1sc0 commented 5 years ago

Managed to resolve in the end. I had made it far more complicated than needed

Resolution was to add all of the JSON key into a single parameter in .env and then use JSON.parse to put into the object.

// .env

GSA_AUTH_KEY={"type": "service_account","project_id": "xxxxx","private_key_id":"xxxxxxx" etc etc}
// gatsby-config.js
require("dotenv").config({
  path: `.env`,
})

var GSAobj = JSON.parse(process.env.GSA_API_KEY)

....
{
    resolve: 'gatsby-source-google-sheets',
    options: {
        spreadsheetId: 'get this from the sheet url',
        worksheetTitle: 'ie the name in the worksheet tab',
        credentials: GSAobj,
    }
},

I then added the same single line parameter in Netlify UI and I have this working both locally and on Netlify (whilst retaining privacy of the key with .env included in .gitignore). Thanks to @krerkkiat for helping me in the right direction. Hope this helps someone else!

frazerf commented 4 years ago

@d1sc0 - thanks so much for this - saved me a tonne of googling! Clear instructions and worked like a dream.