-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGoogleDriveApi.py
More file actions
44 lines (30 loc) · 1.28 KB
/
GoogleDriveApi.py
File metadata and controls
44 lines (30 loc) · 1.28 KB
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
from googleapiclient.discovery import build
from google.oauth2 import service_account
from googleapiclient.http import MediaFileUpload
import os
class GDApi:
def __init__(self):
self.SCOPES = ['https://www.googleapis.com/auth/drive']
self.SERVICE_ACCOUNT_FILE = 'service_account.json'
self.PARENT_FOLDER_ID = "1gDxYl-zN0HgqOXA4rIFXr8YPb4kNdZu4"
def authenticate(self):
creds = service_account.Credentials.from_service_account_file(self.SERVICE_ACCOUNT_FILE, scopes=self.SCOPES)
return creds
def upload(self, file_path):
creds = self.authenticate()
service = build('drive', 'v3', credentials=creds)
file_name = os.path.basename(file_path)
file_metadata = {
'name': file_name,
'parents': [self.PARENT_FOLDER_ID]
}
media = MediaFileUpload(file_path,
mimetype='application/vnd.openxmlformats-officedocument.presentationml.presentation')
file = service.files().create(
body=file_metadata,
media_body=media,
fields='id,webViewLink',
).execute()
web_view_link = file.get('webViewLink')
print("UPLOADED")
return web_view_link