def test_07_ServiceAuthFromSavedCredentialsJsonFile(self): # Have an initial auth so that credentials/7.dat gets saved ga = GoogleAuth(settings_file_path("test_oauth_test_07.yaml")) ga.ServiceAuth() self.assertTrue(os.path.exists(ga.settings["save_credentials_file"])) # Secondary auth should be made only using the previously saved # login info ga = GoogleAuth(settings_file_path("test_oauth_test_07.yaml")) ga.ServiceAuth() self.assertEqual(ga.access_token_expired, False) time.sleep(1)
def main(argv): root_directory = os.getcwd() parent_id = '' try: opts, args = getopt.getopt(argv, 'i:d:h', ["directory="]) except getopt.GetoptError as err: print(err) usage() sys.exit(2) for o, a in opts: if o == "-i": parent_id = a elif o == "-d": root_directory = a elif o == "-h": usage() else: assert False, "unhandled option" if parent_id is not '': gauth = GoogleAuth() gauth.LocalWebserverAuth() drive = GoogleDrive(gauth) if not os.path.exists(root_directory): os.makedirs(root_directory) tree = listFolder(parent_id, drive) downloadAndStore(tree, root_directory)
def test_10_Files_Download_Service(self): """ Tests that a fresh GoogleDrive object can correctly authenticate and download from a file ID. """ drive = GoogleDrive(self.ga) file1 = drive.CreateFile() filename = self.getTempFile("prepatchtestfile") content = "hello world!" file1["title"] = filename file1.SetContentString(content) pydrive_retry(file1.Upload) # Files.insert self.assertEqual(file1.metadata["title"], filename) fileOut1 = self.getTempFile() pydrive_retry(file1.GetContentFile, fileOut1) # fresh download-only instance auth = GoogleAuth( settings_file_path("default.yaml", os.path.join(self.tmpdir, ""))) auth.ServiceAuth() drive2 = GoogleDrive(auth) file2 = drive2.CreateFile({"id": file1["id"]}) fileOut2 = self.getTempFile() pydrive_retry(file2.GetContentFile, fileOut2) self.assertEqual(filecmp.cmp(fileOut1, fileOut2), True) self.DeleteUploadedFiles(drive, [file1["id"]])
def check_google(): from pydrive2.auth import GoogleAuth from pydrive2.drive import GoogleDrive from pydrive2.auth import ServiceAccountCredentials gauth = GoogleAuth() scope = ['https://www.googleapis.com/auth/drive'] gauth.credentials = ServiceAccountCredentials.from_json_keyfile_name( 'credentials.json', scope) drive = GoogleDrive(gauth) file_id = '1vC8oXhfhogAh7olq9BvEPdwwvyeXsZkk' file_list = drive.ListFile({ 'q': "'%s' in parents and trashed=false" % file_id }).GetList() df = pandas.DataFrame(file_list) dfclean = df[['createdDate', 'id', 'title']].copy() dfclean['date'] = pandas.to_datetime(dfclean['createdDate'], format='%Y-%m-%d', errors='coerce') lastupdate = dfclean.loc[dfclean['createdDate'] == '2020-09-28T22:28:33.989Z'].iloc[0]['date'] dfnew = dfclean.loc[dfclean['date'] > lastupdate] all_files = os.listdir('data/tables/') new_files = [ item for item in dfnew['title'].unique().tolist() if item not in all_files ] tabledf = dfnew.loc[dfnew['title'].isin(new_files)] return (tabledf)
def _create_auth(self): """ Create a GoogleAuth object with the correct settings. """ auth = GoogleAuth() auth.settings.update(self.settings) return auth
def getDrive(drive=None, gauth=None): if not drive: if not gauth: gauth = GoogleAuth(settings_file=SETTINGS_YAML) # Try to load saved client credentials gauth.LoadCredentialsFile(CREDENTIALS) if gauth.access_token_expired: # Refresh them if expired try: gauth.Refresh() except RefreshError as e: log.error("Google Drive error: {}".format(e)) except Exception as ex: log.error_or_exception(ex) else: # Initialize the saved creds gauth.Authorize() # Save the current credentials to a file return GoogleDrive(gauth) if drive.auth.access_token_expired: try: drive.auth.Refresh() except RefreshError as e: log.error("Google Drive error: {}".format(e)) return drive
def on_event(self, event, payload): if event == "plugin_backup_backup_created" and self._settings.get_boolean( ["cert_authorized"]): self._logger.info( "{} created, will now attempt to upload to Google Drive". format(payload["path"])) from pydrive2.drive import GoogleDrive from pydrive2.auth import GoogleAuth credentials_file = "{}/credentials.json".format( self.get_plugin_data_folder()) gauth = GoogleAuth() gauth.LoadCredentialsFile(credentials_file) if gauth.credentials is None: self._logger.error("not authorized") self._settings.set(["cert_authorized"], False) self._settings.save() return elif gauth.access_token_expired: gauth.Refresh() else: gauth.Authorize() gauth.SaveCredentialsFile(credentials_file) drive = GoogleDrive(gauth) f = drive.CreateFile({'title': payload["name"]}) f.SetContentFile(payload["path"]) f.Upload() f = None
def check_google(): from pydrive2.auth import GoogleAuth from pydrive2.drive import GoogleDrive from pydrive2.auth import ServiceAccountCredentials gauth = GoogleAuth() scope = ['https://www.googleapis.com/auth/drive'] cred_path = os.path.join(DATA_PATH, 'credentials.json') gauth.credentials = ServiceAccountCredentials.from_json_keyfile_name( cred_path, scope) drive = GoogleDrive(gauth) file_id = '1603ahBNdt1SnSaYYBE-G8SA6qgRTQ6fF' file_list = drive.ListFile({ 'q': "'%s' in parents and trashed=false" % file_id }).GetList() df = pandas.DataFrame(file_list) dfclean = df[['createdDate', 'id', 'title']].copy() dfclean['date'] = pandas.to_datetime(dfclean['createdDate'], format='%Y-%m-%d', errors='coerce') lastupdate = dfclean.loc[dfclean['createdDate'] == '2020-09-11T01:53:29.639Z'].iloc[0]['date'] dfnew = dfclean.loc[dfclean['date'] > lastupdate] all_files = os.listdir(REPORTS_PATH) new_files = [ item for item in all_files if item not in dfnew['title'].unique().tolist() ] reportdf = dfnew.loc[dfnew['title'].isin(new_files)] return (reportdf)
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 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 __init__(self, local_repo_path, model_id): self.repo_path = local_repo_path self.model_id = model_id GoogleAuth = set_secrets_file() gauth = GoogleAuth() gauth.LocalWebserverAuth() self.drive = GoogleDrive(gauth)
def upload_via_celery(self, name, file_name, cred_file): gauth = GoogleAuth() print(os.listdir(settings.MEDIA_ROOT + "/uploaded")) gauth.LoadCredentialsFile(settings.MEDIA_ROOT + "/" + cred_file) self.update_state(state='PROGRESS') # all_objects_dict = {"web_address":[],"original_text":[],"translated_text":[],"name":[],"hyperlink":[],"img":[],"link_to_image":[],"drive_link":[]} all_objects_dict = scrap_the_file(name, self) print(all_objects_dict) dataframe = pd.DataFrame(all_objects_dict) dataframe.drop(columns=["image_data"]) dataframe.columns = [ "Page", "description", "Translated Text", "Name", "hyperlink", "img", "link_to_image", "drive_link" ] dict = dataframe.to_dict() json_str = json.dumps(dict) if os.path.exists(settings.MEDIA_ROOT + "/" + cred_file): os.remove(settings.MEDIA_ROOT + "/" + cred_file) if os.path.exists(settings.MEDIA_ROOT + "/" + cred_file): os.remove(settings.MEDIA_ROOT + "/" + cred_file) return json_str, all_objects_dict["image_data"]
def get_google_auth(): """Create a connection to Google Drive On first use, a client_secrets.json file must be placed in the current directory. If you need guidance on where to get the client_secrets.json file, run `auth-help`. Or consult project readme. Source: https://github.com/wrecodde/pusher#readme If authentication has been previously set up, authentication credentials would be loaded from saved file. """ # TODO: need to allow for expiry and refresh of tokens global drive google_auth = GoogleAuth() if CREDS_FILE.exists(): try: google_auth.LoadCredentialsFile(CREDS_FILE) except: error_message = \ "Error occured when attempting to use saved auth credentials. \n\ Check your internet connection. If the error persists, clear saved credentials with `clear-auth`." logger.error(error_message) else: logger.info('Unavailable auth credentials. Allow access with the `add-auth` command.') sys.exit() drive = GoogleDrive(google_auth)
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()
async def gdrive_upload(filename: str, filebuf: BytesIO = None) -> str: """ Upload files to Google Drive using PyDrive2 """ # a workaround for disabling cache errors # https://github.com/googleapis/google-api-python-client/issues/299 logging.getLogger('googleapiclient.discovery_cache').setLevel( logging.CRITICAL) # Authenticate Google Drive automatically # https://stackoverflow.com/a/24542604 gauth = GoogleAuth() # Try to load saved client credentials gauth.LoadCredentialsFile("secret.json") if gauth.credentials is None: return "nosecret" if gauth.access_token_expired: gauth.Refresh() else: # Initialize the saved credentials gauth.Authorize() # Save the current credentials to a file gauth.SaveCredentialsFile("secret.json") drive = GoogleDrive(gauth) if filename.count('/') > 1: filename = filename.split('/')[-1] filedata = { 'title': filename, "parents": [{ "kind": "drive#fileLink", "id": GDRIVE_FOLDER }] } if filebuf: mime_type = mimetypes.guess_type(filename) if mime_type[0] and mime_type[1]: filedata['mimeType'] = f"{mime_type[0]}/{mime_type[1]}" else: filedata['mimeType'] = 'text/plain' file = drive.CreateFile(filedata) file.content = filebuf else: file = drive.CreateFile(filedata) file.SetContentFile(filename) name = filename.split('/')[-1] file.Upload() # insert new permission file.InsertPermission({ 'type': 'anyone', 'value': 'anyone', 'role': 'reader' }) if not filebuf: os.remove(filename) reply = f"[{name}]({file['alternateLink']})\n" \ f"__Direct link:__ [Here]({file['downloadUrl']})" return reply
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 setup_class(cls): setup_credentials() cls.tmpdir = mkdtemp() cls.ga = GoogleAuth( settings_file_path("default.yaml", os.path.join(cls.tmpdir, ""))) cls.ga.ServiceAuth()
def setup_class(cls): setup_credentials() create_file(cls.first_file, cls.first_file) create_file(cls.second_file, cls.second_file) cls.ga = GoogleAuth(settings_file_path("default.yaml")) cls.ga.ServiceAuth()
def setup_class(cls): setup_credentials() create_file(cls.first_file, cls.first_file) create_file(cls.second_file, cls.second_file) cls.ga = GoogleAuth('pydrive2/test/settings/default.yaml') cls.ga.ServiceAuth()
def google_drive_connect(id, path, auto_auth=True): ''' Method for connecting to the subject's Google Drive ''' # Initialize Google Auth object try: gauth = GoogleAuth(settings_file=path + '/Subjects/' + id + '/pydrive.yaml') local_set = True except: gauth = GoogleAuth() local_set = False # String to the credentials file credPath = path + '/Subjects/' + id + "/mycreds.txt" # Try to load saved client credentials gauth.LoadCredentialsFile(credPath) # If this credential does not exist if gauth.credentials is None: # Authenticate if they're not there raise Exception( "Error: the credentials for this subject do not exist.") elif gauth.access_token_expired: # Refresh them if expired print("Note: This subject's credentials are being renewed.") # try to refresh the access token try: gauth.Refresh() # may fail if the refresh token does not exist except: #gauth.LocalWebserverAuth() #gauth.Authenticate() print("ERROR: Authorization refresh failed!") return None #print("Gauth:") #print(gauth) # Save the current credentials to the file #gauth.SaveCredentialsFile(credPath) # Initialize the Google Drive Connection drive = GoogleDrive(gauth) #print("Drive Object:") #print(drive) # Return the Google Drive object return drive
def create_client(credentials): """ create_engine: Sets up Google Drive API access using credentials (see above). """ gauth = GoogleAuth() gauth.credentials = credentials drive = GoogleDrive(gauth) return drive
def fs(tmpdir, base_remote_dir): setup_credentials() auth = GoogleAuth(settings_file_path("default.yaml", tmpdir / "")) auth.ServiceAuth() bucket, base = base_remote_dir.split("/", 1) fs = GDriveFileSystem(base_remote_dir, auth) fs._gdrive_create_dir("root", base) return fs
def test_01_LocalWebserverAuthWithClientConfigFromFile(self): # Delete old credentials file delete_file("credentials/1.dat") # Test if authentication works with config read from file ga = GoogleAuth(settings_file_path("test_oauth_test_01.yaml")) ga.LocalWebserverAuth() self.assertEqual(ga.access_token_expired, False) # Test if correct credentials file is created self.CheckCredentialsFile("credentials/1.dat") time.sleep(1)
def drive_login() -> GoogleAuth: cwd = change_dir( ) # temp solution until we know what will be the working directory gauth = GoogleAuth() gauth.LocalWebserverAuth() os.chdir(cwd) return gauth
def test_02_LocalWebserverAuthWithClientConfigFromSettings(self): # Delete old credentials file delete_file('credentials/2.dat') # Test if authentication works with config read from settings ga = GoogleAuth('pydrive2/test/settings/test_oauth_test_02.yaml') ga.LocalWebserverAuth() self.assertEqual(ga.access_token_expired, False) # Test if correct credentials file is created self.CheckCredentialsFile('credentials/2.dat') time.sleep(1)
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 test_03_LocalWebServerAuthWithNoCredentialsSaving(self): # Delete old credentials file delete_file('credentials/4.dat') # Provide wrong credentials file ga = GoogleAuth('pydrive2/test/settings/test_oauth_test_03.yaml') ga.LocalWebserverAuth() self.assertEqual(ga.access_token_expired, False) # Test if correct credentials file is created self.CheckCredentialsFile('credentials/4.dat', no_file=True) time.sleep(1)
def __init__(self): settings_file = utils.find_settings_file() if not settings_file: print( "error: no settings file found, please make sure you have authenticate yourself and authorize the CLI with gdrive-auth; exiting..." ) sys.exit() self.gauth = GoogleAuth(settings_file=settings_file) self.drive = GoogleDrive(self.gauth) self.options = parse_command_line() self.config = None
def __init__(self): gauth = GoogleAuth() gauth.LoadCredentialsFile("google_credentials.txt") if gauth.credentials is None: gauth.LocalWebserverAuth() elif gauth.access_token_expired: gauth.Refresh() else: gauth.Authorize() gauth.SaveCredentialsFile("google_credentials.txt") self.drive = GoogleDrive(gauth)
def access(): gauth = GoogleAuth() gauth.LoadCredentialsFile("credentials.json") # load credentials if gauth.credentials is None: # if failed, get them gauth.LocalWebserverAuth() elif gauth.access_token_expired: # or if expired, refresh gauth.Refresh() else: gauth.Authorize() # good gauth.SaveCredentialsFile("credentials.json") # save return GoogleDrive(gauth)