forked from larsks/gitdriver
-
Notifications
You must be signed in to change notification settings - Fork 0
/
drive.py
180 lines (138 loc) · 5.2 KB
/
drive.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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
#!/usr/bin/python
import os
import sys
import argparse
import urllib
import yaml
import json
import requests
OAUTH_URI='https://accounts.google.com/o/oauth2'
VALIDATE_URI='https://www.googleapis.com/oauth2/v1/tokeninfo'
DRIVE_URI='https://www.googleapis.com/drive/v2'
OAUTH_SCOPES = [
'https://www.googleapis.com/auth/userinfo.email',
'https://www.googleapis.com/auth/userinfo.profile',
]
DRIVE_RW_SCOPE = 'https://www.googleapis.com/auth/drive'
DRIVE_RO_SCOPE = 'https://www.googleapis.com/auth/drive.readonly'
REDIRECT_URI='urn:ietf:wg:oauth:2.0:oob'
class GoogleDrive(object):
def __init__(self,
client_id,
client_secret,
credentials=None,
scopes=None):
self.client_id = client_id
self.client_secret = client_secret
self.scopes = OAUTH_SCOPES
self.session = requests.Session()
self.token = None
if scopes is not None:
self.scopes.extend(scopes)
if credentials is None:
credentials = os.path.join(os.environ['HOME'], '.googledrive')
self.credentials = credentials
def authenticate(self):
'''Establish Google credentials. This will load stored credentials
and validate them, and it will call self.login() if stored
credentials are unavailable or fail to validate.'''
self.load_credentials()
if self.token is None:
self.login()
else:
try:
# Always refresh the token. This is a dirty hack to avoid
# doing anything more complicated.
self.refresh()
self.validate()
except ValueError:
self.login()
# Add an Authorization header to all requests made through
# our requests.Session object.
self.session.headers.update({
'Authorization': 'Bearer %(access_token)s' % self.token
})
def refresh(self):
'''Use a refresh_token to refresh the access_token. See
https://developers.google.com/drive/about-auth'''
if not 'refresh_token' in self.token:
raise ValueError('no refresh token')
r = self.session.post('%s/token' % OAUTH_URI, {
'client_id': self.client_id,
'client_secret': self.client_secret,
'refresh_token': self.token['refresh_token'],
'grant_type': 'refresh_token'})
if not r:
raise ValueError('failed to refresh token')
self.token['access_token'] = r.json()['access_token']
self.store_credentials()
def login(self):
'''Perform OAuth authentication.'''
params = {
'client_id': self.client_id,
'scope': ' '.join(OAUTH_SCOPES),
'redirect_uri': REDIRECT_URI,
'access_type': 'offline',
'response_type': 'code',
}
url = '%s?%s' % ('%s/auth' % OAUTH_URI, urllib.parse.urlencode(params))
print('Point your browser at the following URL and then ')
print('enter the authorization code at the prompt:')
print()
print(url)
print()
code = input('Enter code: ')
self.code = code
r = requests.post('%s/token' % OAUTH_URI, {
'code': code,
'client_id': self.client_id,
'client_secret': self.client_secret,
'redirect_uri': REDIRECT_URI,
'grant_type': 'authorization_code',
})
if not r:
raise ValueError('failed to authenticate')
self.token = r.json()
self.store_credentials()
def store_credentials(self):
'''Write credentials to file.'''
with open(self.credentials, 'wb') as fd:
fd.write(yaml.safe_dump(self.token, encoding='utf-8', default_flow_style=False))
def load_credentials(self):
'''Read credentials from file.'''
try:
with open(self.credentials) as fd:
self.token = yaml.load(fd)
except IOError:
pass
def validate(self):
'''Validate token.'''
r = requests.get('%s?access_token=%s' % (
VALIDATE_URI, self.token['access_token']
))
self._validate_response = r
if not r:
raise ValueError('failed to validate')
def files(self):
'''Return an iterator over the files in Google Drive.'''
r = self.session.get('%s/files' % DRIVE_URI).json()
for fspec in r['items']:
yield fspec
def get_file_metadata(self, fid):
'''Return the file metadata for a file identified by its ID.'''
return self.session.get('%s/files/%s' % (DRIVE_URI, fid)).json()
def revisions(self, fid):
'''Return an iterator over the revisions of a file
identified by its ID.'''
r = self.session.get('%s/files/%s/revisions' % (
DRIVE_URI, fid)).json()
for rev in r['items']:
yield rev
if __name__ == '__main__':
cfg = yaml.load(open('gd.conf'))
gd = GoogleDrive(
client_id=cfg['googledrive']['client id'],
client_secret=cfg['googledrive']['client secret'],
scopes=[DRIVE_RW_SCOPE],
)
gd.authenticate()