-
Notifications
You must be signed in to change notification settings - Fork 0
/
googlelogin.py
31 lines (28 loc) · 1.59 KB
/
googlelogin.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
import json
import httplib2
from googleapiclient.discovery import build
class auth:
def __init__(self, json_key=json.load(open(r'path to your json'))):
self.json_key = json_key
self.scope_ga = ['https://www.googleapis.com/auth/analytics.readonly']
self.scope_gs = ['https://spreadsheets.google.com/feeds']
def ga_login(self, api_name='analytics', api_version='v3'):
try:
from oauth2client.client import SignedJwtAssertionCredentials
credentials = SignedJwtAssertionCredentials(self.json_key['client_email'], self.json_key['private_key'].encode(), self.scope_ga)
http = credentials.authorize(httplib2.Http())
service = build(api_name, api_version, http=http)
except ImportError:
from oauth2client.service_account import ServiceAccountCredentials
credentials = ServiceAccountCredentials.from_json_keyfile_name(self.json_key, self.scope_ga)
http = credentials.authorize(httplib2.Http())
service = build(api_name, api_version, http=http)
return service
def gs_login(self):
try:
from oauth2client.client import SignedJwtAssertionCredentials
credentials = SignedJwtAssertionCredentials(self.json_key['client_email'], self.json_key['private_key'].encode(), self.scope_gs)
except ImportError:
from oauth2client.service_account import ServiceAccountCredentials
credentials = ServiceAccountCredentials.from_json_keyfile_name(self.json_key, self.scope_ga)
return credentials