GithubHelp home page GithubHelp logo

gmail_sandbox's Introduction

Gmail Sandbox

googleapis

Allows you to read Gmail and read Google Drive files through code. I expose Google API to where you can easily write more functions later. (sendEmail, WriteFiles, DeleteFiles, etc) It's just a sandbox into building bigger applications that utilizes google OAuth2 for Gmail and JWT for Google Drive.

๐Ÿƒ TLDR

  • Navigate to Google API Console select or create your google project.
  • Enable the APIs: Gmail API and Google Drive API
  • Create Service Account credentials and save it as google-drive-creds.json under the config folder.
  • Create OAuth Client ID credentials, select Application Type: Desktop App, and save it as gmail-credentials.json under the config folder.
  • Share the google drive folder with your service account. Can be pulled from google-drive-creds.json next to client_email
  • Install packages with npm i
  • Run node --trace-warnings gmail_sandbox.js and follow the link to allow access, copy the code and past it back in the node app.
  • Create a .env file and copy/paste everything from .env.sample to it. The only thing missing is GDRIVE_ROOT_FOLDER Which will need to be equal to the unique id pulled from the google drive folder that you shared to the client_email.
  • Test it! node --trace-warnings gmail_sandbox.js

๐Ÿš€ Quick start

1. ๐Ÿ’พ Get the code downloaded

git clone [email protected]:ccollins1544/gmail_sandbox.git

2. ๐Ÿ“œ Create Google Credentials

Enable Google APIs

  • Log into the Gmail account you want to have access to Read Emails and Read Google Drive
  • Navigate to Google API Console
  • Create a NEW PROJECT or select the project you want to connect this application too
  • Double check that your in the correct email and project and then enable the APIs: Gmail API and Google Drive API
  • In the sidebar on the left, expand APIs & auth > Credentials NOTE: Gmail will use OAuth 2.0 Client IDs and Google Drive will use Service Accounts

Create Service Account Credentials for Google Drive API

  • Click "+ Create Credentials" button and select Service Account

  • Follow the creation wizard naming your app something for you to remember.

  • After creation, click the pencil icon to edit and click Add Key button, select Create new key, select the JSON key type option, and finally click Create button. NOTE: Your JSON key is generated and will be downloaded to your machine. That is the only copy!

  • Copy your newly created JSON key file to this app under the config folder and rename it to be google-drive-creds.json NOTE: See Example of what this JSON object should look like here.

  • Share the google drive folder with your service account.

NOTE: Navigate to Google drive and log into the email connected to the project your api key is tied to. Inside of your google-drive-creds.json you should have a client_email copy that! Right click on the folder you want to grant access to the service account, select share, and paste the service account email and done!

Create OAuth 2.0 Client IDs Credentials for Gmail API

  • Click "+ Create Credentials" button and select OAuth Client ID

  • Select Application Type: Desktop App

  • Download your newly created JSON key file and move it to this app under the config folder and rename it to be gmail-credentials.json NOTE: At the time of this writing it's called 'Desktop App' but I've seen multiple tutorials that call it something else. See Example of what this JSON object should look like here.

3. ๐Ÿ“ฆ Install Node Packages

npm i 

4. ๐Ÿฅ‘ Usage

Create a .env file and copy/paste everything from .env.sample to it. The only thing missing is GDRIVE_ROOT_FOLDER Which will need to be equal to the unique id pulled from the google drive folder that you shared to the client_email.

  • Navigate to the folder you shared and copy the last part of the URL. See image for reference.

First Run

node --trace-warnings gmail_sandbox.js

And this should give you a url where you'll need to copy/paste into your browser.

Click allow access and copy the provided code and paste it back into the node app.

If everything went well you should ge a token file generated under .keys/gmail-token.json

Currently you can invoke the following commands

node --trace-warnings gmail_sandbox.js
node gmail_sandbox.js --inputFunction="readGmail" --q="label:inbox"
node gmail_sandbox.js --inputFunction="listFiles" --gDrivePrefix="a"

NOTE: The first one uses the parameters defined in the variable TESTS where enabled: true

๐Ÿ“‚ Example JSON Credentials

OAuth Client ID

{
  "installed": {
    "client_id": "1111",
    "project_id": "asdf",
    "auth_uri": "https://accounts.google.com/o/oauth2/auth",
    "token_uri": "https://oauth2.googleapis.com/token",
    "auth_provider_x509_cert_url": "https://www.googleapis.com/oauth2/v1/certs",
    "client_secret": "",
    "redirect_uris": [
      "urn:ietf:wg:oauth:2.0:oob",
      "http://localhost"
    ]
  }
}

Service Account

{
  "type": "service_account",
  "project_id": "something",
  "private_key_id": "something",
  "private_key": "-----BEGIN PRIVATE KEY-----\n\n-----END PRIVATE KEY-----\n",
  "client_email": "[email protected]",
  "client_id": "1111111111",
  "auth_uri": "https://accounts.google.com/o/oauth2/auth",
  "token_uri": "https://oauth2.googleapis.com/token",
  "auth_provider_x509_cert_url": "https://www.googleapis.com/oauth2/v1/certs",
  "client_x509_cert_url": "https://website.com"
}

๐Ÿ” Find Me

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.