Git Product home page Git Product logo

gmail-oauth2-script's Introduction

Gmail OAuth2 Script

Script to get OAuth2 Access Token for Gmail.

It can be used with cli tools that interacts with Gmail via IMAP or SMTP. For example:

Installation

Just copy the script you want on your machine and give it executable permission:

curl --progress-bar https://raw.githubusercontent.com/MunifTanjim/gmail-oauth2-script/main/gmail-oauth2_none_none.sh -o ~/.local/bin/gmail-oauth2.sh
chmod u+x ~/.local/bin/gmail-oauth2.sh

If you need one, create an OAuth client. It is recommented to use "Desktop app" as OAuth client type.

Variants

There are several variants of the script using different tools for cache, storage or secret management.

gmail-oauth2_none_none.sh

Minimal script with no extra features for caching or secret management. It requires hardcoding sensitive information to the script.

Dependencies

  • bash
  • jq

Usage

Get Refresh Token:

gmail-oauth2.sh refresh_token --client-id CLIENT_ID --client-secret CLIENT_SECRET

Get Access Token:

gmail-oauth2.sh access_token --client-id CLIENT_ID --client-secret CLIENT_SECRET --refresh-token REFRESH_TOKEN

gmail-oauth2_bitwarden_secret-tool.sh

Uses Bitwarden during initial setup and GNOME Keyring for the rest.

Store your OAuth2 Client's credentials in a Bitwarden item with the following custom fields:

  • client_id - OAuth2 Client ID
  • client_secret - OAuth2 Client Secret

Dependencies

Usage

gmail-oauth2.sh <COMMAND> [ACCOUNT_ID]

Commands:
  authorize      Perform the initial authorization steps
  access_token   Prints Access Token

Flags:
  --account-id   Alias for the Gmail Account         (e.g.: "personal" or "[email protected]")
  --store-id     Description for GNOME Keyring Items (default: "Gmail-OAuth2-Script")
  --secret-id    Search Term for Bitwarden Item      (default: "client.oauth2.com.google.mail")

Initial Authorization:

Reads the client_id and client_secret from Bitwarden, stores them in GNOME Keyring. Obtains a refresh_token after successful authorization and stores it in GNOME Keyring.

gmail-oauth2.sh authorize [email protected]
# or
gmail-oauth2.sh authorize --account-id [email protected]

Get Access Token:

Checks GNOME Keyring for existing access_token, if it's not expired prints it. Otherwise, get an access_token using the client_id, client_secret, refresh_token from GNOME Keyring, stores it in GNOME Keyring and prints it.

gmail-oauth2.sh access_token --account-id [email protected]

License

Licensed under the MIT License. Check the LICENSE file for details.

gmail-oauth2-script's People

Contributors

aommm avatar muniftanjim avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

Forkers

cengiz-io aommm

gmail-oauth2-script's Issues

Error: line 65: r: variable not associated

There are an issue after enter the authorization code.

The script return:

./gmail-oauth2.sh: line 65: r: variable not associated

That line inside code is:

let -r refresh_token=$(curl --silent \

If I remove the -r from there, the script is executable and return:

./gmail-oauth2.sh: linha 73: let: refresh_token=1//XXXXXXXXXXXXXXXXXXXX: erro de sintaxe: esperava operando (token de erro é "/0YYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYY")

XXXX = refresh-token
YYYY = access token

With XXXX and YYYY I could run my gmail IMAP in offlineimap. However, there are some mistakes here.

Missing refresh_token error

When I run:

gmail-oauth2.sh access_token --client-id CLIENT_ID --client-secret CLIENT_SECRET --refresh_token REFRESH_TOKEN

I got:

missing: refresh_token

But, the refresh_token is there!

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.