Created
December 7, 2022 19:44
-
-
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
""" | |
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