Giter VIP home page Giter VIP logo

easygoogle's Introduction

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('[email protected]')

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

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

easygoogle's People

Contributors

dependabot[bot] avatar fryuni avatar mend-bolt-for-github[bot] avatar

Stargazers

 avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

Forkers

exhorder

easygoogle's Issues

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.