Coder Social home page Coder Social logo

mbroton / pyioga Goto Github PK

View Code? Open in Web Editor NEW
3.0 1.0 0.0 26 KB

Python IMAP OAuth2 Gmail Authenticator Helper

License: GNU General Public License v3.0

Python 100.00%
google google-api google-api-python-client imap oauth oauth2 python scripts test-automation gmail

pyioga's Introduction

Pyioga

Python IMAP OAuth2 Gmail Authenticator Helper

About The Project

This project helps to setup Google OAuth2 authentication for IMAP connection with Gmail.

If you want to read emails from Gmail in your automated E2E tests or scripts, you're in a right place!

This project is built to guide you through the process. Pyioga is an additional layer built on Google authentication libraries to make it easy.

Getting Started

Google account setup

  • Go to GCP Credentials page
  • Create credentials -> OAuth Client ID -> Web Application
  • Add authorized redirect URI "http://localhost/" (trailing slash is important!)
  • Download credentials json file (client secrets)

Pyioga installation

Install package using pip

pip install pyioga

Generate token file, based on downloaded client secrets file. During this step a web browser is going to pop up with request for permissions.

pyioga init --client-secret-file <path to client secrets file> --output-file <path e.g. token.json>

Having token file, you are ready to go!

Usage

Assuming you went through installation and you have token file (also known as "authorized user file") you can use it to authenticate IMAP connections, using pyioga's functions.

Example with Python's builtin library imaplib

import imaplib
import pyioga

username = "[email protected]"
access_token = pyioga.get_access_token("token.json")
client = imaplib.IMAP4_SSL(host="imap.gmail.com")
client.authenticate("XOAUTH2", authobject=lambda x: pyioga.get_auth_string(username, access_token))
# ('OK', [b'[email protected] authenticated (Success)'])

Example with imap-tools

import pyioga
from imap_tools import MailBox, AND

username = "[email protected]"
access_token = pyioga.get_access_token("token.json")
with MailBox('imap.gmail.com').xoauth2(username, access_token) as mailbox:
    for msg in mailbox.fetch():
        print(msg.date, msg.subject, len(msg.text or msg.html))

pyioga.get_access_token is ensuring you will receive a valid token - otherwise it should raise an exception.

(back to top)

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.