def authenticate_and_authorize(options, settings_file=None, method="local"): gauth = GoogleAuth(settings_file=settings_file) gauth.LoadCredentials() if gauth.credentials is not None: raise RuntimeError( "Error: credentials found at %s and it's already authenticated; skipping..." % gauth.settings.get("save_credentials_file")) if method == "local": gauth.LocalWebserverAuth(host_name=options.hostname, port_numbers=options.ports) elif method == "command_line": gauth.CommandLineAuth() else: raise ValueError( "Error: received --method=%s, but --method can only be either 'local' or 'command_line'." % method) if gauth: print() print("Finished authentication and authorizion.") print( "Please configure google drive client with gdrive-config if you have not done so yet." ) print("Then, use gdrive -h for more information.") return gauth
def get_auth(settings_file="settings.yaml", webauth=False): gauth = GoogleAuth(settings_file=settings_file) if webauth: gauth.LocalWebserverAuth() else: gauth.CommandLineAuth() return gauth
def _drive(self): from pydrive2.auth import RefreshError from pydrive2.auth import GoogleAuth from pydrive2.drive import GoogleDrive if os.getenv(GDriveRemote.GDRIVE_CREDENTIALS_DATA): with open(self._gdrive_user_credentials_path, "w") as credentials_file: credentials_file.write( os.getenv(GDriveRemote.GDRIVE_CREDENTIALS_DATA)) GoogleAuth.DEFAULT_SETTINGS["client_config_backend"] = "settings" if self._use_service_account: GoogleAuth.DEFAULT_SETTINGS["service_config"] = { "client_service_email": self._service_account_email, "client_user_email": self._service_account_user_email, "client_pkcs12_file_path": self._service_account_p12_file_path, } else: GoogleAuth.DEFAULT_SETTINGS["client_config"] = { "client_id": self._client_id or self.DEFAULT_GDRIVE_CLIENT_ID, "client_secret": self._client_secret or self.DEFAULT_GDRIVE_CLIENT_SECRET, "auth_uri": "https://accounts.google.com/o/oauth2/auth", "token_uri": "https://oauth2.googleapis.com/token", "revoke_uri": "https://oauth2.googleapis.com/revoke", "redirect_uri": "", } GoogleAuth.DEFAULT_SETTINGS["save_credentials"] = True GoogleAuth.DEFAULT_SETTINGS["save_credentials_backend"] = "file" GoogleAuth.DEFAULT_SETTINGS[ "save_credentials_file"] = self._gdrive_user_credentials_path GoogleAuth.DEFAULT_SETTINGS["get_refresh_token"] = True GoogleAuth.DEFAULT_SETTINGS["oauth_scope"] = [ "https://www.googleapis.com/auth/drive", "https://www.googleapis.com/auth/drive.appdata", ] # Pass non existent settings path to force DEFAULT_SETTINGS loading gauth = GoogleAuth(settings_file="") try: if self._use_service_account: gauth.ServiceAuth() else: gauth.CommandLineAuth() except RefreshError as exc: raise GDriveAccessTokenRefreshError from exc except KeyError as exc: raise GDriveMissedCredentialKeyError( self._gdrive_user_credentials_path) from exc # Handle pydrive2.auth.AuthenticationError and other auth failures except Exception as exc: raise DvcException("Google Drive authentication failed") from exc finally: if os.getenv(GDriveRemote.GDRIVE_CREDENTIALS_DATA): os.remove(self._gdrive_user_credentials_path) return GoogleDrive(gauth)
def test_04_CommandLineAuthWithClientConfigFromFile(self): # Delete old credentials file delete_file('credentials/1.dat') # Test if authentication works with config read from file ga = GoogleAuth('pydrive2/test/settings/test_oauth_test_04.yaml') ga.CommandLineAuth() self.assertEqual(ga.access_token_expired, False) # Test if correct credentials file is created self.CheckCredentialsFile('credentials/1.dat') time.sleep(1)
def __init__(self, file_path, file_config="/etc/pydrivesync.yaml", gdriveIds=["root"]): self.download_folder = os.path.join(os.path.abspath(os.getcwd()), file_path, self.DRIVE_PATH) if not os.path.exists(self.download_folder): os.mkdir(self.download_folder) gauth = GoogleAuth(settings_file=file_config) gauth.CommandLineAuth() self.drive = GoogleDrive(gauth) self.gdriveIds = gdriveIds temp_file_md5_list = pkg_resources.resource_filename( __name__, "temp_file_md5_list.txt") if os.path.exists(temp_file_md5_list): with open(temp_file_md5_list, 'r') as f: l = f.read() if l is not None and l != "": line = l.split(":") self.current_list_file.append(line[0]) self.current_list_md5.append(line[1]) f.close()
def _drive(self): from pydrive2.auth import GoogleAuth from pydrive2.drive import GoogleDrive if os.getenv(GDriveTree.GDRIVE_CREDENTIALS_DATA): with open(self._gdrive_user_credentials_path, "w") as cred_file: cred_file.write(os.getenv(GDriveTree.GDRIVE_CREDENTIALS_DATA)) auth_settings = { "client_config_backend": "settings", "save_credentials": True, "save_credentials_backend": "file", "save_credentials_file": self._gdrive_user_credentials_path, "get_refresh_token": True, "oauth_scope": [ "https://www.googleapis.com/auth/drive", "https://www.googleapis.com/auth/drive.appdata", ], } if self._use_service_account: auth_settings["service_config"] = { "client_service_email": self._service_account_email, "client_user_email": self._service_account_user_email, "client_pkcs12_file_path": self._service_account_p12_file_path, } else: auth_settings["client_config"] = { "client_id": self._client_id or self.DEFAULT_GDRIVE_CLIENT_ID, "client_secret": self._client_secret or self.DEFAULT_GDRIVE_CLIENT_SECRET, "auth_uri": "https://accounts.google.com/o/oauth2/auth", "token_uri": "https://oauth2.googleapis.com/token", "revoke_uri": "https://oauth2.googleapis.com/revoke", "redirect_uri": "", } GoogleAuth.DEFAULT_SETTINGS.update(auth_settings) # Pass non existent settings path to force DEFAULT_SETTINGS loadings gauth = GoogleAuth(settings_file="") try: logger.debug( "GDrive remote auth with config '{}'.".format( GoogleAuth.DEFAULT_SETTINGS ) ) if self._use_service_account: gauth.ServiceAuth() else: gauth.CommandLineAuth() GDriveTree._validate_credentials(gauth, auth_settings) # Handle AuthenticationError, RefreshError and other auth failures # It's hard to come up with a narrow exception, since PyDrive throws # a lot of different errors - broken credentials file, refresh token # expired, flow failed, etc. except Exception as exc: raise GDriveAuthError(self.credentials_location) from exc finally: if os.getenv(GDriveTree.GDRIVE_CREDENTIALS_DATA): os.remove(self._gdrive_user_credentials_path) return GoogleDrive(gauth)
class Sheet(): def __init__(self, folder_inf=FOLDER_INFORME_ID, folder_rev=FOLDER_REVELAMIENTO_ID, sheet_id=SHEET_ID, remito=REMITO_COL, informe=INFORME_COL, revelamiento=REVELEMIENTO_COL) -> None: #Inicicializando Variables de Settings self.FOLDER_INFORME_ID = folder_inf self.FOLDER_REVELAMIENTO_ID = folder_rev self.SHEET_ID = sheet_id self.REMITO_COL = remito self.INFORME_COL = informe self.REVELEMIENTO_COL = revelamiento #* Variables para guardar en DB self.numero_estudio_col = NUMERO_ESTUDIO_COL self.id_cliente_col = ID_CLIENTE_COL self.nombre_cliente_col = NOMBRE_CLIENTE_COL self.tipo_estudio_col = TIPO_ESTUDIO_COL self.aprob2_col = APROB2_COL #* Variables para envio de correo self.sheet_email_id = SHEET_EMAIL_ID self.id_cliente_email_col = ID_CLIENTE_EMAIL_COL self.correo_cliente_col = CORREO_CLIENTE_COL # Declarando Variables de trabajo self.informes = [] self.revelamientos = [] self.sh = [] self.ws = [] #? Variables de actualizar informe self.col_remito = [] self.col_informe = [] self.col_revelamiento = [] #? Variables de guardar en DataBase self.numero_estudio = [] self.id_cliente = [] self.nombre_cliente = [] self.tipo_estudio = [] self.aprob2 = [] self.id_cliente_email = [] self.correo_cliente = [] # Autenticacion self.gauth = GoogleAuth() self.gc = pygsheets.authorize(client_secret='sheet_secret.json') self.gauth.CommandLineAuth() # Obtener valores de trabajos actuales def get_values(self): self.informes = self.search_folder(self.FOLDER_INFORME_ID) self.revelamientos = self.search_folder(self.FOLDER_REVELAMIENTO_ID) self.sh = self.gc.open_by_key(self.SHEET_ID) self.ws = self.sh.sheet1 self.col_remito = self.ws.get_col(REMITO_COL) self.col_informe = self.ws.get_col(INFORME_COL, value_render="FORMULA") self.col_revelamiento = self.ws.get_col(REVELEMIENTO_COL, value_render="FORMULA") # Requiere ID de una carpeta, retorna lista de archivos y/o sub carpetas def search_folder(self, folderid: str): drive = GoogleDrive(self.gauth) file_list = drive.ListFile({ 'q': f"'{folderid}' in parents and trashed=false" }).GetList() return file_list # Iterar por cada uno de los informes, revisar en la columna remito y escribir los hiperlinks # No retorna nada def escribir_informes(self, folder_list, col_objetivo, col_lista): for folder_element in folder_list: element = folder_element['title'].split('.', 1) if element[0] in self.col_remito: index = self.col_remito.index(element[0]) if not col_lista[index]: link = folder_element["alternateLink"] title = folder_element["title"] col_lista[index] = f'=HYPERLINK("{link}";"{title}")' self.ws.update_col(col_objetivo, col_lista) # Obtiene los valores de todas las variables de interes de la base de datos def get_status_columns(self): self.sh = self.gc.open_by_key(self.SHEET_ID) self.ws = self.sh.sheet1 self.numero_estudio = self.ws.get_col(self.numero_estudio_col) self.id_cliente = self.ws.get_col(self.id_cliente_col) self.nombre_cliente = self.ws.get_col(self.nombre_cliente_col) self.tipo_estudio = self.ws.get_col(self.tipo_estudio_col) self.aprob2 = self.ws.get_col(self.aprob2_col) def get_email_inf(self): self.sh = self.gc.open_by_key(self.sheet_email_id) self.ws = self.sh.sheet1 self.id_cliente_email = self.ws.get_col(self.id_cliente_col) self.correo_cliente = self.ws.get_col(self.correo_cliente_col)
def drive(self): from pydrive2.auth import RefreshError if not hasattr(self, "_gdrive"): from pydrive2.auth import GoogleAuth from pydrive2.drive import GoogleDrive if os.getenv(RemoteGDrive.GDRIVE_USER_CREDENTIALS_DATA): with open( self.gdrive_user_credentials_path, "w" ) as credentials_file: credentials_file.write( os.getenv(RemoteGDrive.GDRIVE_USER_CREDENTIALS_DATA) ) GoogleAuth.DEFAULT_SETTINGS["client_config_backend"] = "settings" GoogleAuth.DEFAULT_SETTINGS["client_config"] = { "client_id": self.client_id, "client_secret": self.client_secret, "auth_uri": "https://accounts.google.com/o/oauth2/auth", "token_uri": "https://oauth2.googleapis.com/token", "revoke_uri": "https://oauth2.googleapis.com/revoke", "redirect_uri": "", } GoogleAuth.DEFAULT_SETTINGS["save_credentials"] = True GoogleAuth.DEFAULT_SETTINGS["save_credentials_backend"] = "file" GoogleAuth.DEFAULT_SETTINGS[ "save_credentials_file" ] = self.gdrive_user_credentials_path GoogleAuth.DEFAULT_SETTINGS["get_refresh_token"] = True GoogleAuth.DEFAULT_SETTINGS["oauth_scope"] = [ "https://www.googleapis.com/auth/drive", "https://www.googleapis.com/auth/drive.appdata", ] # Pass non existent settings path to force DEFAULT_SETTINGS loading gauth = GoogleAuth(settings_file="") try: gauth.CommandLineAuth() except RefreshError as exc: raise GDriveAccessTokenRefreshError( "Google Drive's access token refreshment is failed" ) from exc except KeyError as exc: raise GDriveMissedCredentialKeyError( "Google Drive's user credentials file '{}' " "misses value for key '{}'".format( self.gdrive_user_credentials_path, str(exc) ) ) # Handle pydrive2.auth.AuthenticationError and others auth failures except Exception as exc: raise DvcException( "Google Drive authentication failed" ) from exc finally: if os.getenv(RemoteGDrive.GDRIVE_USER_CREDENTIALS_DATA): os.remove(self.gdrive_user_credentials_path) self._gdrive = GoogleDrive(gauth) self.remote_root_id = self.get_remote_id( self.path_info, create=True ) self._cached_dirs, self._cached_ids = self.cache_root_dirs() return self._gdrive
def __init__(self, parsed_url): duplicity.backend.Backend.__init__(self, parsed_url) try: import httplib2 from apiclient.discovery import build except ImportError as e: raise BackendException(u"""\ PyDrive backend requires PyDrive and Google API client installation. Please read the manpage for setup details. Exception: %s""" % str(e)) # Shared Drive ID specified as a query parameter in the backend URL. # Example: pydrive://developer.gserviceaccount.com/target-folder/?driveID=<SHARED DRIVE ID> self.api_params = {} self.shared_drive_id = None if u'driveID' in parsed_url.query_args: self.shared_drive_id = parsed_url.query_args[u'driveID'][0] self.api_params = { u'corpora': u'teamDrive', u'teamDriveId': self.shared_drive_id, u'includeTeamDriveItems': True, u'supportsTeamDrives': True } try: from pydrive2.auth import GoogleAuth from pydrive2.drive import GoogleDrive from pydrive2.files import ApiRequestError, FileNotUploadedError except ImportError as e: try: from pydrive.auth import GoogleAuth from pydrive.drive import GoogleDrive from pydrive.files import ApiRequestError, FileNotUploadedError except ImportError as e: raise BackendException(u"""\ PyDrive backend requires PyDrive installation. Please read the manpage for setup details. Exception: %s""" % str(e)) # let user get by with old client while he can try: from oauth2client.client import SignedJwtAssertionCredentials self.oldClient = True except: from oauth2client.service_account import ServiceAccountCredentials from oauth2client import crypt self.oldClient = False if u'GOOGLE_DRIVE_ACCOUNT_KEY' in os.environ: account_key = os.environ[u'GOOGLE_DRIVE_ACCOUNT_KEY'] if self.oldClient: credentials = SignedJwtAssertionCredentials( parsed_url.username + u'@' + parsed_url.hostname, account_key, scopes=u'https://www.googleapis.com/auth/drive') else: signer = crypt.Signer.from_string(account_key) credentials = ServiceAccountCredentials( parsed_url.username + u'@' + parsed_url.hostname, signer, scopes=u'https://www.googleapis.com/auth/drive') credentials.authorize(httplib2.Http()) gauth = GoogleAuth(http_timeout=60) gauth.credentials = credentials elif u'GOOGLE_DRIVE_SETTINGS' in os.environ: gauth = GoogleAuth( settings_file=os.environ[u'GOOGLE_DRIVE_SETTINGS'], http_timeout=60) gauth.CommandLineAuth() elif (u'GOOGLE_SECRETS_FILE' in os.environ and u'GOOGLE_CREDENTIALS_FILE' in os.environ): gauth = GoogleAuth(http_timeout=60) gauth.LoadClientConfigFile(os.environ[u'GOOGLE_SECRETS_FILE']) gauth.LoadCredentialsFile(os.environ[u'GOOGLE_CREDENTIALS_FILE']) if gauth.credentials is None: gauth.CommandLineAuth() elif gauth.access_token_expired: gauth.Refresh() else: gauth.Authorize() gauth.SaveCredentialsFile(os.environ[u'GOOGLE_CREDENTIALS_FILE']) else: raise BackendException( u'GOOGLE_DRIVE_ACCOUNT_KEY or GOOGLE_DRIVE_SETTINGS environment ' u'variable not set. Please read the manpage to fix.') self.drive = GoogleDrive(gauth) if self.shared_drive_id: parent_folder_id = self.shared_drive_id else: # Dirty way to find root folder id file_list = self.drive.ListFile({ u'q': u"'Root' in parents and trashed=false" }).GetList() if file_list: parent_folder_id = file_list[0][u'parents'][0][u'id'] else: file_in_root = self.drive.CreateFile( {u'title': u'i_am_in_root'}) file_in_root.Upload() parent_folder_id = file_in_root[u'parents'][0][u'id'] file_in_root.Delete() # Fetch destination folder entry and create hierarchy if required. folder_names = parsed_url.path.split(u'/') for folder_name in folder_names: if not folder_name: continue list_file_args = { u'q': u"'" + parent_folder_id + u"' in parents and trashed=false" } list_file_args.update(self.api_params) file_list = self.drive.ListFile(list_file_args).GetList() folder = next( (item for item in file_list if item[u'title'] == folder_name and item[u'mimeType'] == u'application/vnd.google-apps.folder'), None) if folder is None: create_file_args = { u'title': folder_name, u'mimeType': u"application/vnd.google-apps.folder", u'parents': [{ u'id': parent_folder_id }] } create_file_args[u'parents'][0].update(self.api_params) create_file_args.update(self.api_params) folder = self.drive.CreateFile(create_file_args) if self.shared_drive_id: folder.Upload(param={u'supportsTeamDrives': True}) else: folder.Upload() parent_folder_id = folder[u'id'] self.folder = parent_folder_id self.id_cache = {}
example usage: upload.py 0B5XXXXY9KddXXXXXXXA2c3ZXXXX /path/to/my/file """ import sys from os import path from pydrive2.auth import GoogleAuth from pydrive2.drive import GoogleDrive from pydrive2.settings import LoadSettingsFile # Update this value to the correct location. # e.g. "/usr/local/scripts/pydrive/settings.yaml" PATH_TO_SETTINGS_FILE = None assert PATH_TO_SETTINGS_FILE is not None # Fail if path not specified. gauth = GoogleAuth() gauth.settings = LoadSettingsFile(filename=PATH_TO_SETTINGS_FILE) gauth.CommandLineAuth() drive = GoogleDrive(gauth) # If provided arguments incorrect, print usage instructions and exit. if len(sys.argv) < 2: print("usage: upload.py <Google Drive folder ID> <local file path>") exit(1) # Exit program as incorrect parameters provided. parentId = sys.argv[1] myFilePath = sys.argv[2] myFileName = path.basename(sys.argv[2]) # Check if file name already exists in folder. file_list = drive.ListFile({ "q": '"{}" in parents and title="{}" and trashed=false'.format(