Giter VIP home page Giter VIP logo

Comments (6)

d1sc0 avatar d1sc0 commented on July 18, 2024 5

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!

from gatsby-source-google-sheets.

frazerf avatar frazerf commented on July 18, 2024 1

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

from gatsby-source-google-sheets.

krerkkiat avatar krerkkiat commented on July 18, 2024

Maybe this is what you are looking for?

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

from gatsby-source-google-sheets.

d1sc0 avatar d1sc0 commented on July 18, 2024

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.

from gatsby-source-google-sheets.

krerkkiat avatar krerkkiat commented on July 18, 2024

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

from gatsby-source-google-sheets.

d1sc0 avatar d1sc0 commented on July 18, 2024

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

from gatsby-source-google-sheets.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.