Skip to main content

Easy to use wrapper to google APIs client library

Project description

Easy Google APIs

CircleCI PyPiStatus Coverage Status GitHub

PyPi PyPiVersions

Google APIs python library wrapepr

Python package to make Google APIs more practical and easy to use
Wraps Google's official API library most used authentication and authorization flow

Supports OAuth authentication and service accounts (directly and with domain wide delegation)

Installation

pip install -U easygoogle

Prerequisites

To use Google APIs you'll need an credentials json file from Google Cloud Platform. To generate it, follow this steps:

  1. Ensure you have a GCP (Google Cloud Platform) Project
  2. Go to the GCP Console and select the project that you want to use on the top-left corner on the right of the title and the three dots
  3. Go to the API Library and enable the APIs you intend to use
  4. On the APIs & services Dashboard, where you should arrive, click on Credentials on the left menu
To use OAuth2 authentication:
  1. Click on the blue button Create credentials and select OAuth Client ID
  2. Select your application type, name it and click Create (if you are running utilities scripts, you probably want the Other type)
  3. Click OK on the popup, find the name you just used and click the download button at the right end of the line

Alternatively, you can use the application default credentials defined by Google Cloud SDK command gcloud auth application-default login

To use a Service Account authentication:
  1. Click on the blue button Create credentials and select Service account key
  2. Select the service account you want to use or create a new one
  • If you are going to use Domain wide Delegation, you need to ensure the service account has the role Project > Service Account Actor and is authorized to impersonate the account of the GSuite Panel you want. Follow the official documentation on how to do it.
  1. Select the keyfile type JSON
  2. Save the file

Usage

OAuth2 authentication:
user = easygoogle.oauth2(
    json_file, # Path to the JSON file
    scopes, # List of scopes identifiers
    appname='Google Client Library - Python', # Optional. Used to identify the credentials file saved
    user='', # Optional. Used to identify the credentials file saved
    app_dir='.', # Path to create subdir '.credentials' and store credentials files. Defaults to current working directory
    manualScopes=[], # Manually defined scopes for authorization. Used in Single Sign-On with servers that support OAuth authentication
    hostname='localhost', # Where to open authentication flow server
    port=None, # Which port to open authentication flow server in. Defaults to a random available port
    auth_mode=easygoogle.AUTH.BROWSER, # Select authorization flow to be used in case of missing authorized credentials.
    # Options are available on easygoogle.AUTH namespace:
    # BROWSER -> Open the authorization page on default browser
    # SILENTLY -> Shows the authorization link on stdout, but do not open browser automatically
    # CONSOLE -> Shows the authorization link on stdout, but do not open browser automatically, and asks for authorization code on stdin
)

# To use the application default credentials from gcloud sdk
user = easygoogle.oauth2.default()

# Build API as the authenticated user
api = user.get_api(
    apiname # API identifier
    version=None, # API version. Defaults to primary version on Google Discovery Docs
)

Example:

import easygoogle

# Build the easygoogle controller
account_controller = easygoogle.oauth2('oauth_secret.json', ['drive'])

# Build Drive API
drive = account_controller.get_api('drive')

# Print result from listing drive content
print(drive.files().list().execute())
Service Account authentication:
service = easygoogle.service_acc(
    json_file, # Path to the JSON file
    scopes, # List of scopes identifiers,
    domainWide=False, # Set true to enable the .delegate option
    manualScopes=[] # Manually defined scopes for authorization. Used in Single Sign-On with servers that support OAuth authentication
)

# To use application default credentials service account
service = easygoogle.service_acc.default(
    scopes=['cloud-platform'], # List of scopes identifiers,
    domainWide=False, # Set true to enable the .delegate option
    manualScopes=[] # Manually defined scopes for authorization. Used in Single Sign-On with servers that support OAuth authentication
)

# Build API as the service account
api = service.get_api(
    apiname # API identifier
    version=None, # API version. Defaults to primary version on Google Discovery Docs
)


# Build user OAuth2 credentials with user impersonation
user = service.delegate(
    user_email # Email or alias of the impersonating user
)

# Build API as the impersonated user
api = user.get_api(
    apiname # API identifier
    version=None, # API version. Defaults to primary version on Google Discovery Docs
)

Example:

import easygoogle

service = easygoogle.service_acc('service_secret.json', ['drive'])

# Building Drive API as the service account
serviceDrive = servie.get_api('drive')

print(serviceDrive.files().list().execute())
# List files on the service account individual Drive


# Acquiring credentials for user
user = service.delegate('user@example.org')

# Building Drive API as the impersonated user
userDrive = user.get_api('drive')

print(userDrive.files().list().execute())
# List files on the user individual Drive

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

easygoogle-2.0.0.dev3.tar.gz (13.9 kB view details)

Uploaded Source

Built Distributions

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

easygoogle-2.0.0.dev3-py3.7.egg (19.6 kB view details)

Uploaded Egg

easygoogle-2.0.0.dev3-py3.6-none-any.whl (24.4 kB view details)

Uploaded Python 3

File details

Details for the file easygoogle-2.0.0.dev3.tar.gz.

File metadata

  • Download URL: easygoogle-2.0.0.dev3.tar.gz
  • Upload date:
  • Size: 13.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.14.0 pkginfo/1.5.0.1 requests/2.22.0 setuptools/41.2.0 requests-toolbelt/0.9.1 tqdm/4.35.0 CPython/3.7.4

File hashes

Hashes for easygoogle-2.0.0.dev3.tar.gz
Algorithm Hash digest
SHA256 b7f60544a46af0f15c48e12d50944d47dafc645177dc52062bf513df0683955f
MD5 5ea70edc6879060f0d385cf7c05b5ed5
BLAKE2b-256 5fa848b1fa622f15a0930f21133eeae03c8dba8bf137cc14520d58352bddf11e

See more details on using hashes here.

File details

Details for the file easygoogle-2.0.0.dev3-py3.7.egg.

File metadata

  • Download URL: easygoogle-2.0.0.dev3-py3.7.egg
  • Upload date:
  • Size: 19.6 kB
  • Tags: Egg
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.14.0 pkginfo/1.5.0.1 requests/2.22.0 setuptools/41.2.0 requests-toolbelt/0.9.1 tqdm/4.35.0 CPython/3.7.4

File hashes

Hashes for easygoogle-2.0.0.dev3-py3.7.egg
Algorithm Hash digest
SHA256 ae5e7c7847bfd52e64338753a3a7d3ed0c7bc05cac4bf8e96630dc040c3326f1
MD5 372ac970808a2d835e4037496b653a5d
BLAKE2b-256 b785d22efedea37734e9718b9194ada36ab783d45fee8f4515b9d17fbf5af5dd

See more details on using hashes here.

File details

Details for the file easygoogle-2.0.0.dev3-py3.6-none-any.whl.

File metadata

  • Download URL: easygoogle-2.0.0.dev3-py3.6-none-any.whl
  • Upload date:
  • Size: 24.4 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.14.0 pkginfo/1.5.0.1 requests/2.22.0 setuptools/41.2.0 requests-toolbelt/0.9.1 tqdm/4.35.0 CPython/3.7.4

File hashes

Hashes for easygoogle-2.0.0.dev3-py3.6-none-any.whl
Algorithm Hash digest
SHA256 8dd1eaa3b754cf33a61f02df3d77eec88f00beb764f9fb58d3502f49cf5ac059
MD5 586c00876ff6fc15e5f4b45d8b2b8c79
BLAKE2b-256 5eed4ac93cb6d551c97caab9f5c0f08cf7bab4d85c4ded628e14479f726e3876

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