Skip to main content

A python library for authenticating requests for various google services including ``gmail``, ``youtube``, ``drive`` and ``calendar``.

Project description

gVerify

Overview

A python library for authenticating requests for various google services including gmail, youtube, drive and calendar.

Requirements

  • Python 3.10+
  • Works on Linux, Windows, macOS, BSD

Getting started

Getting an access key

To get started with this library, you need a google account in order to obtain access credentials. First, pick a service that you want to use, which could be: - gmail - youtube - drive - calendar

Installing the library

The next step is to instal the library from pypi:

pip install gverify

Authorizing a request

In this case we will use youtube as the service. We will authorize a request to search youtube for python programming videos:

from gverify import GoogleOAuth, YouTubeScopes

client_secret_file: str = 'client_secret.json'
api_service_name: str = 'youtube'
api_version: str = 'v3'
credentials_dir: str = '.youtube'
scopes: list[str] = [YouTubeScopes.youtube.value]
oauth: GoogleOAuth = GoogleOAuth(
    secrets_file=secret_file,
    scopes=scopes,
    api_service_name=api_service_name,
    api_version=api_version,
    credentials_dir=credentials_dir
)
youtube_client = oauth.authenticate_google_server()
youtube_find_request = youtube_client.search().list(q='python programming videos', part='id, snippet')
print(youtube_find_request.execute())

Using the Flask Framework

Currently, this library does not intergrate with web frameworks like Flask and fastAPI. To use flask, we use the underlying library:

from flask import Flask, url_for, redirect, request, session
from google_auth_oauthlib.flow import Flow
from google.oauth2.credentials import Credentials
import os


CLIENT_SECRETS_FILE = "client_secret.json"
SCOPES = [
    'https://www.googleapis.com/auth/youtube.force-ssl',
    'https://www.googleapis.com/auth/youtube'
]
API_SERVICE_NAME = 'youtube'
API_VERSION = 'v3'

app: Flask = Flask(__name__)
app.secret_key = 'secret-key'


@app.route('/')
def home():
    if not session['credentials']:
        redirect(url_for('authorize'))
    return 'Home'


@app.route('/authorize')
def authorize():
    flow = Flow.from_client_secrets_file(
      CLIENT_SECRETS_FILE,
      scopes=SCOPES
      )
    flow.redirect_uri = url_for('oauth2callback', _external=True)
    authorization_url, state = flow.authorization_url(
      access_type='offline',
      include_granted_scopes='true'
    )
    session['state'] = state
    return redirect(authorization_url)


def credentials_to_dict(credentials: Credentials) -> dict:
  return {
            'token': credentials.token,
          'refresh_token': credentials.refresh_token,
          'token_uri': credentials.token_uri,
          'client_id': credentials.client_id,
          'client_secret': credentials.client_secret,
          'scopes': credentials.scopes
          }


@app.route('/oauth2callback')
def oauth2callback():
    state = session['state']
    flow = Flow.from_client_secrets_file(
      CLIENT_SECRETS_FILE,
      scopes=SCOPES,
      state=state
    )
    flow.redirect_uri = url_for('oauth2callback', _external=True)
    authorization_response = request.url
    flow.fetch_token(authorization_response=authorization_response)
    credentials: Credentials = flow.credentials
    session['credentials'] = credentials_to_dict(credentials)
    return redirect('/')


if __name__ == '__main__':
    os.environ['OAUTHLIB_INSECURE_TRANSPORT'] = '1'
    app.run('localhost', 5000, debug=True)

How to Contribute

To contribute, chack out the contribution guideline.

License

The API uses an MIT License

Developer

Lyle Okoth – @lyleokoth on twitter

lyle okoth on medium

My email is lyceokoth@gmail.com

Here is my GitHub Profile

You can also find me on Linkedin

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

gverify-0.2.1.tar.gz (6.2 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

gverify-0.2.1-py3-none-any.whl (6.3 kB view details)

Uploaded Python 3

File details

Details for the file gverify-0.2.1.tar.gz.

File metadata

  • Download URL: gverify-0.2.1.tar.gz
  • Upload date:
  • Size: 6.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.12.3

File hashes

Hashes for gverify-0.2.1.tar.gz
Algorithm Hash digest
SHA256 b7ba80a54562e4e03d5c3de04d679aff36e8a5510ed76664db8f4f4cb94d8cc4
MD5 8aa7b917c24f3caa4ae6a427f63ad5e6
BLAKE2b-256 33251bc774865f9d23000b855db9291fdf7f182e933373fe605a609a0529656c

See more details on using hashes here.

File details

Details for the file gverify-0.2.1-py3-none-any.whl.

File metadata

  • Download URL: gverify-0.2.1-py3-none-any.whl
  • Upload date:
  • Size: 6.3 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.12.3

File hashes

Hashes for gverify-0.2.1-py3-none-any.whl
Algorithm Hash digest
SHA256 a381722710f24dfa35084adb9563d55f4a6b1a5d4eb9512d09fdf399edd26a72
MD5 73ad0e3b0322bcd2febe0dfa4c9ecd0d
BLAKE2b-256 adefe5012e23a9f15cf27fedb63b1ebedd7a0e04ff57e4cd62223274b49c48e0

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page