-
Notifications
You must be signed in to change notification settings - Fork 3
/
googleapi.py
executable file
·83 lines (64 loc) · 2.04 KB
/
googleapi.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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
#!/usr/bin/python3
# python
import http.client
import httplib2
import os
import random
import sys
import time
import webbrowser
import threading
import pprint
# google
from googleapiclient.discovery import build
from googleapiclient.errors import HttpError
from googleapiclient.http import MediaFileUpload
from oauth2client.client import OAuth2WebServerFlow
from oauth2client.file import Storage
from oauth2client.tools import argparser, run_flow
class GoogleAPIBase():
storage_file = "./token.json"
client_id = None
client_secret = None
api_name = None
api_version = None
service = None
credentials = None
scopes = []
def set_client(self, client_id, client_secret):
GoogleAPIBase.client_id = client_id
GoogleAPIBase.client_secret = client_secret
def set_api(self, name, version):
self.api_name = name
self.api_version = version
def set_storage(self, storage):
self.storage_file = storage
def add_scope(self, scope):
GoogleAPIBase.scopes.append(scope)
GoogleAPIBase.credentials = None
def build_scope(self):
scope = ""
for s in GoogleAPIBase.scopes:
scope += s + " "
return scope
def login(self):
flow = OAuth2WebServerFlow(
client_id=self.client_id,
client_secret=self.client_secret,
scope=self.build_scope(),
redirect_uri="http://localhost"
)
storage = Storage(self.storage_file)
GoogleAPIBase.credentials = storage.get()
if GoogleAPIBase.credentials is None or \
GoogleAPIBase.credentials.invalid:
flags = argparser.parse_args(args=[])
GoogleAPIBase.credentials = run_flow(flow, storage, flags)
def get_service(self):
if GoogleAPIBase.credentials is None:
self.login()
return build(
self.api_name, self.api_version,
http=GoogleAPIBase.credentials.authorize(httplib2.Http()))
def connect(self):
self.service = self.get_service()