Skip to content

Instantly share code, notes, and snippets.

@tales-aparecida
Created December 7, 2022 19:44
Show Gist options
  • Save tales-aparecida/4332e14ae66aa96cacc35f186d0ea41a to your computer and use it in GitHub Desktop.
Save tales-aparecida/4332e14ae66aa96cacc35f186d0ea41a to your computer and use it in GitHub Desktop.
Use Google Drive authentication to define who is running a notebook in Google Colab
"""
Use Google Drive authentication to define who is running this notebook.
"""
import re
from pathlib import Path
from google.colab import drive
from google.colab.drive import _logs_dir
# mount Google Drive
drive.mount('/content/drive') # this will ask for permission
def _get_google_drive_authorized_user():
try:
path_to_drive_log = Path(_logs_dir()) / 'drive_fs.txt'
drive_log = path_to_drive_log.read_text()
authorization_match = re.search("Authorized as (.+)@", drive_log)
username = authorization_match.group(1)
except Exception as error:
print(
"Couldn't find username based on Google Drive authentication."
' Using "anonymous".'
)
username = "anonymous"
return username
username = _get_google_drive_authorized_user()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment