-
Notifications
You must be signed in to change notification settings - Fork 14
/
google_auth.py
36 lines (32 loc) · 1.22 KB
/
google_auth.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
32
33
34
35
36
from oauth2client.service_account import ServiceAccountCredentials
from googleapiclient.discovery import build
from httplib2 import Http
class google_auth:
credential_path = "./credential.json"
types = {
'analytics_v4': {
'api_name': 'analyticsreporting',
'scope': 'https://www.googleapis.com/auth/analytics',
'version': 'v4'
},
'bigquery_v2': {
'api_name': 'bigquery',
'scope': 'https://www.googleapis.com/auth/bigquery',
'version': 'v2'
},
'storage_v1': {
'api_name': 'storage',
'scope': 'https://www.googleapis.com/auth/cloud-platform',
'version': 'v1'
}
}
def __init__(self, path):
self.credential_path = path
def get_auth(self, name):
if name in self.types:
credentials = ServiceAccountCredentials.from_json_keyfile_name(self.credential_path, self.types[name]['scope'])
http_auth = credentials.authorize(Http())
auth_object = build(self.types[name]['api_name'], self.types[name]['version'], http=http_auth)
return auth_object
else:
raise LookupError('There is no such type!')