Create app/google_drive.py
Browse files- app/google_drive.py +73 -0
app/google_drive.py
ADDED
|
@@ -0,0 +1,73 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
import logging
|
| 3 |
+
import google_auth_oauthlib.flow
|
| 4 |
+
import googleapiclient.discovery
|
| 5 |
+
import googleapiclient.http
|
| 6 |
+
|
| 7 |
+
logger = logging.getLogger(__name__)
|
| 8 |
+
|
| 9 |
+
# Load Google OAuth config from environment variables
|
| 10 |
+
GOOGLE_OAUTH_CONFIG = {
|
| 11 |
+
"web": {
|
| 12 |
+
"client_id": os.environ.get("GOOGLE_CLIENT_ID"),
|
| 13 |
+
"project_id": os.environ.get("GOOGLE_PROJECT_ID"),
|
| 14 |
+
"auth_uri": os.environ.get("GOOGLE_AUTH_URI", "https://accounts.google.com/o/oauth2/auth"),
|
| 15 |
+
"token_uri": os.environ.get("GOOGLE_TOKEN_URI", "https://oauth2.googleapis.com/token"),
|
| 16 |
+
"auth_provider_x509_cert_url": "https://www.googleapis.com/oauth2/v1/certs",
|
| 17 |
+
"client_secret": os.environ.get("GOOGLE_CLIENT_SECRET"),
|
| 18 |
+
"redirect_uris": [os.environ.get("GOOGLE_REDIRECT_URI")]
|
| 19 |
+
}
|
| 20 |
+
}
|
| 21 |
+
|
| 22 |
+
def get_google_auth_url():
|
| 23 |
+
"""Generate URL for Google Auth"""
|
| 24 |
+
client_config = GOOGLE_OAUTH_CONFIG["web"]
|
| 25 |
+
flow = google_auth_oauthlib.flow.Flow.from_client_config(
|
| 26 |
+
{"web": client_config},
|
| 27 |
+
scopes=["https://www.googleapis.com/auth/drive.file"]
|
| 28 |
+
)
|
| 29 |
+
flow.redirect_uri = client_config["redirect_uris"][0]
|
| 30 |
+
authorization_url, _ = flow.authorization_url(
|
| 31 |
+
access_type="offline",
|
| 32 |
+
include_granted_scopes="true",
|
| 33 |
+
prompt="consent"
|
| 34 |
+
)
|
| 35 |
+
return authorization_url
|
| 36 |
+
|
| 37 |
+
def exchange_code_for_credentials(auth_code):
|
| 38 |
+
"""Exchange authorization code for credentials"""
|
| 39 |
+
if not auth_code.strip():
|
| 40 |
+
return None, "No code provided."
|
| 41 |
+
try:
|
| 42 |
+
client_config = GOOGLE_OAUTH_CONFIG["web"]
|
| 43 |
+
flow = google_auth_oauthlib.flow.Flow.from_client_config(
|
| 44 |
+
{"web": client_config},
|
| 45 |
+
scopes=["https://www.googleapis.com/auth/drive.file"]
|
| 46 |
+
)
|
| 47 |
+
flow.redirect_uri = client_config["redirect_uris"][0]
|
| 48 |
+
flow.fetch_token(code=auth_code.strip())
|
| 49 |
+
creds = flow.credentials
|
| 50 |
+
if not creds or not creds.valid:
|
| 51 |
+
return None, "Could not validate credentials. Check code and try again."
|
| 52 |
+
return creds, "Google Sign-In successful!"
|
| 53 |
+
except Exception as e:
|
| 54 |
+
return None, f"Error during token exchange: {e}"
|
| 55 |
+
|
| 56 |
+
def google_drive_upload(file_path, credentials, folder_id=None):
|
| 57 |
+
"""Upload a file to Google Drive"""
|
| 58 |
+
try:
|
| 59 |
+
drive_service = googleapiclient.discovery.build("drive", "v3", credentials=credentials)
|
| 60 |
+
file_metadata = {'name': os.path.basename(file_path)}
|
| 61 |
+
if folder_id:
|
| 62 |
+
file_metadata['parents'] = [folder_id]
|
| 63 |
+
media = googleapiclient.http.MediaFileUpload(file_path, resumable=True)
|
| 64 |
+
created = drive_service.files().create(body=file_metadata, media_body=media, fields='id').execute()
|
| 65 |
+
return created.get("id", "")
|
| 66 |
+
except Exception as e:
|
| 67 |
+
return f"Error uploading to Drive: {str(e)}"
|
| 68 |
+
|
| 69 |
+
def create_drive_folder(drive_service, name):
|
| 70 |
+
"""Create a folder in Google Drive"""
|
| 71 |
+
folder_metadata = {'name': name, 'mimeType': 'application/vnd.google-apps.folder'}
|
| 72 |
+
folder = drive_service.files().create(body=folder_metadata, fields='id').execute()
|
| 73 |
+
return folder.get('id')
|