simple wrapper on Google API connections
Project description
Google API Connection Wrapper
General Connector Classes for Google Products
Current Wrappers Available
Wrappers In the Pipeline
STEPS
- Acquire Google Credentials from API Console
- Install this pacakge
- Create Connection in Python
- Use wrapper to make API calls
Acquire Google Credentials from API Console
First we will need to get our own Google Project set up so we can get our credentials. If you don't have experience, you can do so here Google API Console
After you have your project set up, oAuth configured, and the optional service account (only for Google Big Query connections), you are good to install this package.
Make sure to download your oAuth credentials and save them to your working directory as 'client_secret.json'.
Installation
pip install googlewrapper
Establish your Connection
Option 1 - Assign Connection Variable
Use the Connection class found in connect.py to asisgn credentials. It is recomended to do this, if you want to use credentials to authenticate with other libraries.
'client_secret.json' should be in the working directory, if not, please declare the path while initializing the class. See the example below for both versions.
from googlewrapper.connect import Connection
#in working directory
google_connection = Connection()
#declare path in class
google_connection = Connection(file/path/to/client_secret.json)
Once we have our connection object, we wil need to declare the scope of our access. You can do this by accessing the following class methods:
Google Service | Module | Authentication Type | Credential File |
---|---|---|---|
Analytics | .ga() | oAuth | client_secret.json |
Search Console | .gsc() | oAuth | client_secret.json |
Calendar | .cal() | oAuth | client_secret.json |
Big Query | .gbq() | Service Account | gbq-sa.json |
PageSpeed | n/a | API Key | n/a |
Gmail | .gmail() | oAuth | client_secret.json |
Sheets | .gs() | oAuth | client_secret.json |
Note, you can change the file path for authenticating Google Big Query by passing in the Service Account json in the gbq method
gbq_connection = Connection().gbq(file/path/to/service_account.json)
Option 2 - Default Connection (One Line Connect)
It is possible to just use one line when connecting. It is reccommended to do this if you will not need your authentication object, and will just be using the wrapper class.
This can be done by initializing the wrapper classes, without any arguments. By default, each class will authenticate with the default method found in the connect class.
IMPORTANT: To do this, we must have 'client_secret.json' in our working directory. -- for GBQ your 'gbq-sa.json' must be in the working directory
See below.
from googlewrapper.gsc import GoogleSearchConsole
gsc = GoogleSearchConsole()
After authentication has taken place (via either option), a folder will be created in your cwd named credentials. The respective authentication scopes will be stored there so you don't have to authenticate everytime. Each token is stored with the Google property name as a .dat file.
Product Specific Methods
Now that we have our credential object, we are ready to call the api. We will walk through examples for the different products
Google Analytics
Initialize
from googlewrapper.connect import Connection
from googlewrapper.ga import GoogleAnalytics
ga_auth = Connection().ga()
ga = GoogleAnalytics(ga_auth)
Methods
Examples
Google Search Console
Initialize
from googlewrapper.connect import Connection
from googlewrapper.gsc import GoogleSearchConsole
gsc_auth = Connection().gsc()
gsc = GoogleSearchConsole(gsc_auth)
Methods
Examples
#assign the constant variables
sites = list_of_sites
gsc.set_sites(sites)
gsc.set_start_date(day)
gsc.set_end_date(day)
gsc.set_branded(branded_list)
gsc.set_dimensions(dim_list)
#call the api
data = gsc.get_data()
Google Calendar
Initialize
from googlewrapper.connect import Connection
from googlewrapper.cal import GoogleCalendar
cal_auth = Connection().cal()
cal = GoogleCalendar(cal_auth)
Methods
.set_default(cal_id)
- Assigns which calendar will be used to create and find events
.find_event(str)
- Searches for an event in your calendar by name
- Returns events list response object
.get_event(eventId:str)
- Returns the event object of the eventId you passed in
.all_events(num_events,min_date)
- Params
- num_events
- Is the number of events you'd like to return
- defaults to 250
- min_date
- the starting point will only search forward in time from this date
- defaults to the current date and time
- num_events
- Returns
.update_event(new_event,send_updates)
- Params
- new_event
- event formated json to update
- send_updates: str
- if you want to send the updates
- see Google's Docs for more info
- defaults to 'all'
- new_event
- Returns the updated event object
Examples
my_event = cal.find_event('Team Meeting')
Google Big Query
Initialize
from googlewrapper.connect import Connection
from googlewrapper.gbq import GoogleBigQuery
gbq_auth = Connection().gbq()
gbq = GoogleBigQuery(gba_auth)
Remember that Google Big Query authenticates from a service account, be sure to include the gbq-sa.json file in your path, or pass in the pass to the .gbq() method.
Methods
Examples
Google PageSpeed Insights
Initialize
from googlewrapper.pagespeed import PageSpeed
page_speed = PageSpeed(API_KEY)
Methods
Examples
Gmail
Initialize
from googlewrapper.connect import Connection
from googlewrapper.gmail import Gmail
gmail_auth = Connection().gmail()
mail = Gmail(gmail_auth)
Methods
Examples
Google Sheets
Initialize
from googlewrapper.connect import Connection
from googlewrapper.gs import GoogleSheets
gs_auth = Connection().gs()
gs = GoogleSheets(gs_auth)
Methods
Examples
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distributions
Built Distribution
Hashes for googlewrapper-0.1.5rc0-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | c4bf483111817b1be96ed2c364a169626e753fdd8d153861ac32fa5d197634a5 |
|
MD5 | 6468f4c3dd70e6f639d30a00de8e754a |
|
BLAKE2b-256 | 49c7935365aaa04e0badd8ea84b02587c1ea040d556f9ad39c02292313cdf696 |