forked from gati/django-gcalsync
-
Notifications
You must be signed in to change notification settings - Fork 0
/
connect.py
31 lines (25 loc) · 1.05 KB
/
connect.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 httplib2
from oauth2client.file import Storage
from oauth2client.client import SignedJwtAssertionCredentials
from apiclient.discovery import build
from django.conf import settings
class Connection(object):
service = None
def get_service(self):
if self.service:
return self.service
else:
http = httplib2.Http()
storage = Storage(settings.GCALSYNC_CREDENTIALS)
credentials = storage.get()
if credentials is None or credentials.invalid == True:
f = file(settings.GCALSYNC_CREDENTIALS_KEY, 'rb')
key = f.read()
f.close()
credentials = SignedJwtAssertionCredentials(settings.GCALSYNC_CREDENTIALS_EMAIL, key , scope="https://www.googleapis.com/auth/calendar")
storage.put(credentials)
else:
credentials.refresh(http)
http = credentials.authorize(http)
self.service = build("calendar", "v3", http=http)
return self.service