GithubHelp home page GithubHelp logo

damiansire / google-sheet-auth-javascript-examples-google-api Goto Github PK

View Code? Open in Web Editor NEW
0.0 1.0 0.0 10 KB

This repository provides examples and guidance on how to authenticate with Google APIs and interact with Google Sheets using JavaScript/Node.js. Includes demos for reading, writing, and updating spreadsheet data with Google's official `googleapis` library.

JavaScript 100.00%
auth authentication google-sheets google-sheets-api google-sheets-api-v4 javascript nodejs

google-sheet-auth-javascript-examples-google-api's Introduction

Obtaining Google Service Account Credentials

This project uses Google Service Accounts to securely authenticate with Google APIs. Follow these steps to obtain and use your credentials:

1. Create a Service Account:

  1. Visit the Google Cloud Console: Create Service Account Key page.
  2. From the Service account dropdown, select New service account.
  3. Give your service account a descriptive name (e.g., "Spreadsheet Data Reader").
  4. Optionally, assign a role to the service account that gives it the necessary permissions for your task (e.g., "Sheets Viewer" if you only need to read data).
  5. Click Create.
  6. In the "Grant this service account access to project" dialog, you can optionally grant more specific permissions. Be mindful of the principle of least privilege and only grant what's necessary.
  7. Click Continue.
  8. In the "Grant users access to this service account" dialog, you can add other users who should be able to manage this service account. This is optional.
  9. Click Done.

2. Download the Key File:

  1. On the Service Accounts page, you'll see your newly created service account.
  2. Click on the email address of the service account.
  3. Go to the Keys tab.
  4. Click Add Key and select Create new key.
  5. Choose the JSON key type.
  6. Click Create. Your key file will be downloaded automatically.

Important Security Note:

  • Keep your JSON key file confidential! Do not share it publicly or commit it to version control. Treat it like a password.

3. Use the Credentials in Your Code:

Option A: Environment Variable

  1. Rename the downloaded key file to credentials.json (or any name you prefer).

  2. Set the GOOGLE_APPLICATION_CREDENTIALS environment variable to the full path of the key file:

    export GOOGLE_APPLICATION_CREDENTIALS="/path/to/your/credentials.json"

Option B: keyFile Property

  1. In your Node.js code, provide the path to the key file when creating your GoogleAuth instance:

    const { google } = require("googleapis");
    
    const auth = new google.auth.GoogleAuth({
      keyFile: "/path/to/your/credentials.json", // Update with the actual path
      scopes: [
        "[https://www.googleapis.com/auth/spreadsheets.readonly](https://www.googleapis.com/auth/spreadsheets.readonly)",
      ], // Or the necessary scopes for your tasks
    });

google-sheet-auth-javascript-examples-google-api's People

Contributors

damiansire avatar

Watchers

 avatar

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.