def get_credentials(): # Saves valid credentials and points the app to the # correct Google Drive account and folders # = the content (messages to save) """Gets valid user credentials from storage. If nothing has been stored, or if the stored credentials are invalid, the OAuth2 flow is completed to obtain the new credentials. Returns: Credentials, the obtained credential. """ # from google API console - load credentials from file gauth = GoogleAuth() # Object to hold authentication information # Try to load saved client credentials gauth.LoadCredentialsFile("credentials.txt") if gauth.credentials is None: print "No creds file" # Authenticate if they're not there gauth.CommandLineAuth() elif gauth.access_token_expired: # Refresh them if expired gauth.Refresh() else: # Initialize the saved creds gauth.Authorize() # Save the current credentials to a file gauth.SaveCredentialsFile("credentials.txt") return GoogleDrive(gauth) # To be passed as needed
class Uploader: def __init__(self, dir_path, image_files): self.dir_path = dir_path self.image_files = image_files # type: List[ImageFile] self.gauth = GoogleAuth() self.drive = GoogleDrive(None) self.fonder_id = "1hXqzPVaN-YaTjU8zf9v5mZzzUJsxhYrR" def login(self) -> None: self.gauth.CommandLineAuth() self.drive = GoogleDrive(self.gauth) def run(self) -> None: for image_file in self.image_files: file = self.drive.CreateFile({ 'title': image_file.name, 'mimeType': image_file.type, 'parents': [{ 'kind': 'drive#fileLink', 'id': self.fonder_id }] }) file.SetContentFile(self.dir_path + "" + image_file.name) file.Upload()
def __init__(self): gauth = GoogleAuth() gauth.CommandLineAuth() self.drive = GoogleDrive(gauth) logging.config.fileConfig('pyDriveLog.conf', disable_existing_loggers=0) self.logger = logging.getLogger('pydrivelogger')
def authorize(silent=False): """ This function authorizes the application. First, we check to see if there are cached credentials. If that is not the case, or if the credentials are no longer valid, we must then continue the OAuth work flow. :param silent: :boolean: whether this function should be expected to run silently or not. If it is silent and user interaction is needed (e.g. for OAuth stuff), we raise an exception. :returns: :GoogleAuth: gauth object """ gauth = GoogleAuth() gauth.LoadCredentialsFile(CREDENTIALS_CACHE) if not gauth.credentials: if not silent: # Prompts user to complete authorization process gauth.CommandLineAuth() gauth.SaveCredentialsFile(CREDENTIALS_CACHE) else: raise GDriveTransferException( 'User interaction needed. Failing...') return gauth
def main(): # GoogleDriveを扱うオブジェクトを作成 gauth = GoogleAuth() gauth.CommandLineAuth() drive = GoogleDrive(gauth) # スクショを撮る print("Take a picture, cheese!") filename = "room_image.jpg" snap_shot(filename) # wait to save the file # time.sleep(20) # 過去にアップロードしたファイルタグをとってくる print("searching old file...") f = get_file(drive, filename) # ファイルがなければ新規に,あればそれに上書きする if f is None: print("Old file is not found. To make new file.") f = drive.CreateFile() # さっき撮った画像に移し替えてアップロード print("Uploading file...") f.SetContentFile(filename) f["title"] = filename f.Upload()
def upload_to_drive(file_path): """upload_to_drive func upload file to drive Args: file_path(str): upload file path """ gauth = GoogleAuth() gauth.CommandLineAuth() drive = GoogleDrive(gauth) f = drive.CreateFile() print("file path 1: {}".format(file_path)) # file_path = file_path.encode('utf-8') file_path = file_path.encode('utf-16', 'surrogatepass') # file_path = file_path.encode('cp932') print("file path 2: {}".format(file_path)) # file_path = file_path.decode("utf-8") # file_path = file_path.decode("Shift_JIS") file_path = file_path.decode('utf-16', 'surrogatepass') print("file path 3: {}".format(file_path)) f.SetContentFile(file_path) f.Upload()
def G_upload_ScatterFig(local_file, gdrive_dir, scatter_name): print("Start Upload") for i in range(2): try: gauth = GoogleAuth() gauth.CommandLineAuth() drive = GoogleDrive(gauth) folder_id = drive.ListFile({ 'q': 'title = "{}"'.format(gdrive_dir) }).GetList()[0]['id'] f = drive.CreateFile({ 'mimeType': 'image/png', 'parents': [{ 'id': folder_id }] }) f['title'] = scatter_name f.SetContentFile(local_file) f.Upload() print("success!") except Exception as e: traceback.print_exc() print(e) print("Error (count=" + str(i + 1) + ")") time.sleep(3.0) print("Retry") pass else: break else: print("failure...") pass
def get_gauth(self): gauth = None packge_path, _ = os.path.split(__file__) client_config = os.path.join(packge_path, 'client_secrets.json') credentials_file = os.path.join(packge_path, 'drive_credentials') if os.path.exists(credentials_file): try: gauth = GoogleAuth() gauth.LoadClientConfigFile(client_config_file=client_config) gauth.LoadCredentialsFile(credentials_file=credentials_file) return gauth except Exception as e: t.log_message(str(e)) gauth = None if self.config.gdrive_folders is not None and self.config.telegram_channels is not None and self.telegram_bot is not None: try: gauth = self.start_auth_telegram(client_config=client_config) gauth.SaveCredentialsFile(credentials_file=credentials_file) except Exception as e: t.log_message(str(e)) gauth = None elif self.config.gdrive_folders is not None and self.telegram_bot is None or self.config.telegram_channels is None: try: gauth = GoogleAuth() gauth.LoadClientConfigFile(client_config_file=client_config) gauth.CommandLineAuth() gauth.SaveCredentialsFile(credentials_file=credentials_file) except Exception as e: t.log_message(str(e)) gauth = None return gauth
def get_GoogleAuth(self): gauth = GoogleAuth() #Locate File in this directory. Dir_Path = os.path.dirname( os.path.abspath(inspect.getfile(inspect.currentframe()))) Credentials_File = os.path.join(Dir_Path, 'gDriveCred.txt') # Try to load saved client credentials gauth.LoadCredentialsFile(Credentials_File) if gauth.credentials is None: # Authenticate if they're not there if (_platform == "win32") or (_platform == "darwin"): #Use Web Browser to Authenticate gauth.LocalWebserverAuth() else: #Use CLI to Authenticate gauth.CommandLineAuth() # Save the current credentials to a file gauth.SaveCredentialsFile(Credentials_File) #Invoke the function again (recursion) gauth = get_GoogleAuth() elif gauth.access_token_expired: # Refresh them if expired gauth.Refresh() else: # Initialize the saved creds gauth.Authorize() #return object return gauth
def _gdrive_auth(self): if not self._gdoc_config['com_line_auth']: self._gdoc_config['com_line_auth'] = False gauth = GoogleAuth() if self._gdoc_config['com_line_auth']: gauth.CommandLineAuth() self._gdrive = GoogleDrive(gauth) else: if True: # 'False' while debugging to reduce amount of new tabs gauth.LocalWebserverAuth() self._gdrive = GoogleDrive(gauth) else: gauth.LoadCredentialsFile('client_creds.txt') if gauth.credentials is None: gauth.LocalWebserverAuth() elif gauth.access_token_expired: gauth.Refresh() else: gauth.Authorize() gauth.SaveCredentialsFile('client_creds.txt') self._gdrive = GoogleDrive(gauth)
def auth_gdrive(): """Authenticate to Google Drive API""" # Define the credentials folder home_dir = os.path.expanduser("~") credential_dir = os.path.join(home_dir, ".credentials") if not os.path.exists(credential_dir): os.makedirs(credential_dir) credential_path = os.path.join(credential_dir, "pydrive-credentials.json") gauth = GoogleAuth() gauth.LoadCredentialsFile(credential_path) if gauth.credentials is None: # Authenticate if they're not there gauth.CommandLineAuth() elif gauth.access_token_expired: # Refresh them if expired gauth.Refresh() else: # Initialize the saved creds gauth.Authorize() # Save the current credentials to a file gauth.SaveCredentialsFile(credential_path) drive = GoogleDrive(gauth) return drive
def drive(self): from pydrive.auth import RefreshError if not hasattr(self, "_gdrive"): from pydrive.auth import GoogleAuth from pydrive.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 pydrive.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.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): print('Initializing google api...') #再帰関数の上限を変更 sys.setrecursionlimit(300) #oauth認証 try: gauth = GoogleAuth() gauth.CommandLineAuth() self.drive = GoogleDrive(gauth) except Exception as e: print('Oauth authentication failed') print(e) sys.exit(1) #settings.yamlの読み込み try: with open('paperless_setting.yaml') as file: self.config = yaml.safe_load(file.read()) file.close() except Exception as e: print('Error reading paperless _ setting.yaml') print(e) print( 'Please check if paperless _ setting.yml exists in the setting folder.' ) sys.exit(1) print('Finish Initializing.')
def get_gdrive(): settings_yaml = os.getenv('PYDRIVE_SETTINGS_YAML') client_config_file = os.getenv('PYDRIVE_CLIENT_CONFIG_FILE') saved_credentials_file = os.getenv('PYDRIVE_SAVED_CREDENTIALS_FILE') assert (client_config_file and saved_credentials_file) or settings_yaml, \ 'env vars must be set: PYDRIVE_SETTINGS_YAML ' \ 'or PYDRIVE_CLIENT_CONFIG_FILE ' \ 'and PYDRIVE_SAVED_CREDENTIALS_FILE' # import these only if function gets called, so non-colab users don't need to # install/configure PyDrive if they're not going to download f/ Colab. from pydrive.auth import GoogleAuth from pydrive.drive import GoogleDrive if settings_yaml: gauth = GoogleAuth(settings_file=settings_yaml) else: gauth = GoogleAuth() gauth.settings['client_config_backend'] = 'file' gauth.settings['client_config_file'] = client_config_file gauth.settings['save_credentials'] = True gauth.settings['save_credentials_backend'] = 'file' gauth.settings['save_credentials_file'] = saved_credentials_file gauth.settings['get_refresh_token'] = True gauth.CommandLineAuth() drive = GoogleDrive(gauth) return drive
def _gen_drive(): settings_file_path = os.path.join( os.path.dirname(os.path.abspath(__file__)), 'settings.yaml') gauth = GoogleAuth(settings_file=settings_file_path) gauth.CommandLineAuth() drive = GoogleDrive(gauth) return drive
def upload_file(filename): gauth = GoogleAuth() #authenticates application from 'client_secrets.json' gauth.LoadCredentialsFile("/var/lib/cloud9/mycreds.txt") #Load credentials of gdrive user to uplaod files if gauth.credentials is None: #if file is empty or credentials not valid gauth.CommandLineAuth() #For the first time, key has to be entered manually #But should never reach here on crontab job (signal will terminate application after 20s), elif gauth.access_token_expired: #if credentials expired, then refresh them gauth.Refresh() else: #if stored credentials are good, then authenticate application user gauth.Authorize() gauth.SaveCredentialsFile("/var/lib/cloud9/mycreds.txt") #save new credentials drive = GoogleDrive(gauth) file2 = drive.CreateFile() #create file to upload file2.SetContentFile("/var/lib/cloud9/photos/" + str(filename) + ".jpg") #write to file file2['title'] = str(filename) + ".jpg" #file name file2.Upload() #upload file to gdrive print 'Created file %s on Google Drive' % (file2['title']) f = open('/var/lib/cloud9/photos/successful_uploads.txt', 'a') #log if file uploaded successfully. if stuck on credentials, signal termiantes app and nothing is written. f.write(filename+".jpg"+"\n") f.close()
def authenticate(secrets, credentials): authenticator = GoogleAuth() authenticator.settings = { 'client_config_backend': 'file', 'client_config_file': secrets, 'save_credentials': False, 'oauth_scope': ['https://www.googleapis.com/auth/drive'] } authenticator.LoadCredentialsFile(credentials) if authenticator.credentials is None: response = input( 'No credentials. Authenticate with local web browser? [y]/n > ' ) if response.lower() in ['y', 'yes'] or len(response) == 0: authenticator.LocalWebserverAuth() else: authenticator.CommandLineAuth() elif authenticator.access_token_expired: authenticator.Refresh() else: authenticator.Authorize() authenticator.SaveCredentialsFile(credentials) return authenticator
class GoogleStorage: def __init__(self, target_path, settings_file): self.path = target_path self.gauth = None self.config = settings.LoadSettingsFile(settings_file) def connect(self): try: self.gauth = GoogleAuth() # Try to load saved client credentials self.gauth.LoadCredentialsFile( self.config['save_credentials_file']) if self.gauth.credentials is None: if sys.stdout.isatty(): # Authenticate if they're not there and shell is interactive self.gauth.CommandLineAuth() else: raise ValueError( 'No credentials found, need to log in first. try running script from interactive shell' ) elif self.gauth.access_token_expired: # Refresh them if expired self.gauth.Refresh() else: # Initialize the saved creds self.gauth.Authorize() # Save the current credentials to a file self.gauth.SaveCredentialsFile( self.config['save_credentials_file']) except Exception as e: logger.error('failed to connect to google storage', exc_info=True) raise e
def gdrive_init(encoding="utf8"): """From https://github.com/lucasrodes/whatstk/blob/bcb9cf7c256df1c9e270aab810b74ab0f7329436/whatstk/utils/gdrive.py#L38""" if not os.path.isdir(CONFIG_DIR): os.makedirs(CONFIG_DIR, exist_ok=True) # Copy credentials to config folder # copyfile(client_secret_file, CLIENT_SECRETS_PATH) # Create settings.yaml file dix = { "client_config_backend": "file", "client_config_file": CLIENT_SECRETS_PATH, "save_credentials": True, "save_credentials_backend": "file", "save_credentials_file": CREDENTIALS_PATH, "get_refresh_token": True, "oauth_scope": [ "https://www.googleapis.com/auth/drive", "https://www.googleapis.com/auth/drive.install", ], } with open(SETTINGS_PATH, "w", encoding=encoding) as f: yaml.dump(dix, f) # credentials.json gauth = GoogleAuth(settings_file=SETTINGS_PATH) gauth.CommandLineAuth() return gauth
def drive(update: Update, context: CallbackContext): """Called by the /drive command. Lets the user navigate the drive folders, if he has the permissions Args: update: update event context: context passed by the handler """ check_log(update, "drive") chat_id = update.message.chat_id gauth = GoogleAuth(settings_file="config/settings.yaml") gauth.CommandLineAuth() gdrive = GoogleDrive(gauth) if chat_id < 0: context.bot.sendMessage( chat_id=chat_id, text="La funzione /drive non è ammessa nei gruppi") return try: file_list = gdrive.ListFile({ 'q': "'0B7-Gi4nb88hremEzWnh3QmN3ZlU' in parents and trashed=false", 'orderBy': 'folder,title' }).GetList() except AuthError as e: log_error(header="drive", error=e) keyboard = get_files_keyboard( file_list, row_len=3) # keyboard that allows the user to navigate the folder context.bot.sendMessage(chat_id=chat_id, text="DMI UNICT - Appunti & Risorse:", reply_markup=InlineKeyboardMarkup(keyboard))
def drive_write(title, text): gauth = GoogleAuth() gauth.CommandLineAuth() drive = GoogleDrive(gauth) f = drive.CreateFile({'title': title}) f.SetContentString(text) f.Upload()
def upload_file(file_name: str): google_auth = GoogleAuth() # client_secrets.json need to be in the same directory as the script google_auth.CommandLineAuth() drive = GoogleDrive(google_auth) file = drive.CreateFile({'title': os.path.basename(file_name)}) file.SetContentFile(file_name) file.Upload()
def raw_drive(self): from pydrive.auth import GoogleAuth from pydrive.drive import GoogleDrive GoogleAuth.DEFAULT_SETTINGS["client_config_backend"] = "settings" gauth = GoogleAuth(settings_file=self.GOOGLE_AUTH_SETTINGS_PATH) gauth.CommandLineAuth() return GoogleDrive(gauth)
def test_04_CommandLineAuthWithClientConfigFromFile(self): # Delete old credentials file self.DeleteOldCredentialsFile('credentials/1.dat') # Test if authentication works with config read from file ga = GoogleAuth('settings/test4.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_gdrive(settings=None,usecmdline=False): if settings is None: settings_file = os.path.join(os.path.dirname(os.path.realpath(__file__)), 'settings.yaml') logging.debug("Settings file: "+ settings_file) gauth = GoogleAuth(settings_file=settings_file) if usecmdline: gauth.CommandLineAuth() else: gauth.LocalWebserverAuth() return GoogleDrive(gauth)
def __init__(self, settings_file=None): if settings_file is None: settings_file = os.path.join(os.path.dirname(__file__), "settings.yaml") GoogleAuth.DEFAULT_SETTINGS["client_config_backend"] = "settings" gauth = GoogleAuth(settings_file=settings_file) gauth.CommandLineAuth() self.drive = GoogleDrive(gauth) self.root = GDriveFile("/", "root") self._load_dir(self.root)
def drive_init(): gauth = GoogleAuth() os.chdir(SCRIPT_PATH) try: gauth.CommandLineAuth() except pydrive.settings.InvalidConfigError: print( f"Couldn't find '{CLIENT_SECRETS_JSON_PATH}'\n" "Please create it, according to https://medium.com/analytics-vidhya/pydrive-to-download-from-google-drive-to-a-remote-machine-14c2d086e84e" ) sys.exit(1) return GoogleDrive(gauth)
def download(filename): gauth = GoogleAuth() gauth.CommandLineAuth() drive = GoogleDrive(gauth) if filename == 'data': id = 'FILEID' elif filename == 'namelist': id = 'FILEID' elif filename == 'raw_data': id = 'FILEID' f = drive.CreateFile({'id': id}) f.GetContentFile(filename)
def auth_google(): # Google Oauth 認証を行う gauth = GoogleAuth() gauth.LoadCredentialsFile('credentials.json') if gauth.credentials is None: # Authenticate if they're not there # gauth.LocalWebserverAuth() gauth.CommandLineAuth() elif gauth.access_token_expired: # Refresh them if expired gauth.Refresh() else: # Initialize the saved creds gauth.Authorize() # Save the current credentials to a file gauth.SaveCredentialsFile('credentials.json') drive = GoogleDrive(gauth) """ # Git > WeatherExtractor 内のファイル一覧を ID と共に表示 drive_folder_id = '1X_lNjACsoUWbnz3Y-PSoP5e4hplV_f4d' query = '"{0}" in parents and trashed=false'.format(drive_folder_id) file_list = drive.ListFile({'q': query}).GetList() for file1 in file_list: print('title: %s, id: %s' % (file1['title'], file1['id'])) print(file1) """ # WeatherResult.csv を操作してみる # drive_csv_id = '1sCCMDIAMfHTt013R28lVxQj31VlgMopV' # file = drive.CreateFile({'id': drive_csv_id}) # file = drive.CreateFile({'title': 'a.csv', # 'parents': [{'id': '1X_lNjACsoUWbnz3Y-PSoP5e4hplV_f4d'}]}) drive_folder_id = '1X_lNjACsoUWbnz3Y-PSoP5e4hplV_f4d' query = '"{0}" in parents and trashed=false'.format(drive_folder_id) file_list = drive.ListFile({'q': query}).GetList() for file in file_list: if file['title'] == 'WeatherResult.csv': # WeatherResult.csv を取得してダウンロードする content = file.GetContentString() file2 = file # print(file2) break # print(content) # print(type(content)) file2.GetContentFile('a.csv') # csv ファイルとしてダウンロード file2.Trash() # WeatherResult.csv をゴミ箱に移動 file2.UnTrash() # ゴミ箱の外に移動? file2.Delete() # Hard Delete # file.GetContentFile('a.txt') return drive
def cargarFiles(self): gauth = GoogleAuth() gauth.CommandLineAuth() path_to_upload_file = "" drive = GoogleDrive(gauth) file_upload = drive.CreateFile({ 'title': 'test_file.txt', 'mimeType': 'text/plain' }) file_upload.SetContentFile(path_to_upload_file) file_upload.Upload() return True