def start_auth_telegram(self, client_config): if self.telegram_bot is None: t.log_message('telegram bot is None. Telegram auth canceled.') return auth = GoogleAuth() auth.LoadClientConfigFile(client_config_file=client_config) if auth.flow is None: auth.GetFlow() auth.flow.redirect_uri = OOB_CALLBACK_URN self.telegram_bot.send_message( 'Please go to the following link in your browser and send me a Google verification code. \nAuth url: ' + auth.GetAuthUrl()) dirty = False code = None save_credentials = auth.settings.get('save_credentials') if auth.credentials is None and save_credentials: auth.LoadCredentials() if auth.credentials is None: code = self.telegram_bot.get_code() dirty = True else: if auth.access_token_expired: if auth.credentials.refresh_token is not None: auth.Refresh() else: code = self.telegram_bot.get_code() dirty = True if code is not None: auth.Auth(code) if dirty and save_credentials: auth.SaveCredentials() return auth
def save_drive_credential(chat_id, code): try: gauth = GoogleAuth() gauth.Auth(code) gauth.SaveCredentialsFile(f'json/{chat_id}.json') return '授權成功!' except Exception as e: return '授權失敗!可能是格式錯了之類的QQ,麻煩再試一次' # def upload_drive(keyword, chat_id): # # # check folder是否已存在 # folder_name = '股票每日開資訊' # folder_id = None # folder_list = drive.ListFile({'q': "mimeType = 'application/vnd.google-apps.folder' and trashed = false"}).GetList() # for folder in folder_list: # if folder['title'] == folder_name: # folder_id = folder['id'] # break # if not folder_id: # folder = drive.CreateFile({'title': folder_name, 'mimeType': 'application/vnd.google-apps.folder'}) # folder.Upload() # folder_id = folder['id'] # date = '_'.join(map(str, time.localtime()[0:3])) # filename = f'{date}_{keyword}.csv' # # lcoal永遠都存temp.csv,上傳到雲端的再有custom filename # file = drive.CreateFile({'title': filename, 'parents': [{'kind': 'drive#fileLink', 'id': folder_id}]}) # file.SetContentFile(filename='temp.csv') # file.Upload()
def launch(code: str): gauth = GoogleAuth() gauth.GetFlow() gauth.flow.redirect_uri = "http://localhost:8000/launch/" gauth.Auth(code) drive = GoogleDrive(gauth) trio.run(fns_process_files, drive) return "success"
def google_auth_submit(request): google_auth_code = request.POST.get('google') user_creds = AccessToken.objects.get(user=request.user) gauth = GoogleAuth() gauth.Auth(google_auth_code) user_creds.token = gauth.credentials.to_json() user_creds.save() return redirect('/dashboard/config/')
def save_drive_credential(chat_id, code, s3): try: gauth = GoogleAuth() gauth.Auth(code) gauth.SaveCredentialsFile(f'{chat_id}.json') s3.upload_file(Filename=f'{chat_id}.json', Bucket=confidential.Bucket, Key=f'user_credentials/{chat_id}.json') return '授權成功!' except Exception as e: return '授權失敗!可能是格式錯了之類的QQ,麻煩再試一次'
def drive_auth(): gauth = GoogleAuth() # Try to load saved client credentials #gauth.client_config_file = client_secrets_path print(gauth.client_config) if (os.path.exists(creds_path)): gauth.LoadCredentialsFile(creds_path) if gauth.credentials is None: with open(client_secrets_path) as f: data = json.load(f) data = data['web'] print(data) client_id = data['client_id'] client_secret = data['client_secret'] redirect_ris = data['redirect_uris'] auth_uri = data['auth_uri'] ''' gauth.client_config['client_id']=client_id gauth.client_config['client_secret']=client_secret #gauth.client_config['redirect_uris']=redirect_uris gauth.client_config['auth_uri']=auth_uri gauth.client_config['access_type']='offline' ''' # print(gauth.client_config) # Authenticate if they're not there url = "https:www.google.com" #webbrowser.open_new_tab(url) auth_url = gauth.GetAuthUrl() # Create authentication url user needs to visit code = CustomLocalWebserverAuth(auth_url) # Your customized authentication flow gauth.Auth(code) # Authorize and build service from the code #gauth.LocalWebserverAuth() 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(creds_path) print(gauth.credentials) drive = GoogleDrive(gauth) return drive
def __init__(self, model_dir, key="sample"): """ Log in to Google Drive account using OAuth key """ self.model_dir = model_dir self.is_activated = True if key == "sample" or key == None: self.is_activated = False return None auth = GoogleAuth() try: auth.Auth(key) except Exception: message = "Something wrong with your OAuth key. Try to get new one and restart this script" raise GoogleDriveAuthError( message) self.drive = GoogleDrive(auth)
def input_auth_code( code ): logger.log( 'checking auth code' ) client_secrets_path = BASE_DIR + "/client_secrets.json" GoogleAuth.DEFAULT_SETTINGS['client_config_file'] = client_secrets_path # Create google account authentication objects gauth = GoogleAuth() # Input the auth code gauth.Auth( code ) # Save the credentials if they're valid gauth.SaveCredentialsFile( CRED_PATH ) save_credentials_time() logger.log( 'credentials saved and validated' )
class GoogleDriveAdapter: def __init__(self, access_token: str = '', credentials_file: str = 'credentials.json'): self.gauth = GoogleAuth() if access_token: self.gauth.Auth(access_token) else: if os.path.exists(credentials_file): self.gauth.LoadCredentialsFile(credentials_file) else: self.gauth.CommandLineAuth() self.gdrive = GoogleDrive(self.gauth) @property def token(self) -> str: return self.gauth.credentials.access_token
def setup(self): #create authorization helper and load default settings gauth = GoogleAuth( xbmc.validatePath( xbmc.translatePath(utils.addon_dir() + '/resources/lib/pydrive/settings.yaml'))) gauth.LoadClientConfigSettings() #check if this user is already authorized if (not xbmcvfs.exists( xbmc.translatePath(utils.data_dir() + "google_drive.dat"))): settings = { "client_id": self.CLIENT_ID, 'client_secret': self.CLIENT_SECRET } drive_url = gauth.GetAuthUrl(settings) utils.log("Google Drive Authorize URL: " + drive_url) code = xbmcgui.Dialog().input( 'Google Drive Validation Code', 'Input the Validation code after authorizing this app') gauth.Auth(code) gauth.SaveCredentialsFile( xbmc.validatePath( xbmc.translatePath(utils.data_dir() + 'google_drive.dat'))) else: gauth.LoadCredentialsFile( xbmc.validatePath( xbmc.translatePath(utils.data_dir() + 'google_drive.dat'))) #create the drive object self.drive = GoogleDrive(gauth) #make sure we have the folder we need xbmc_folder = self._getGoogleFile(self.root_path) print xbmc_folder if (xbmc_folder == None): self.mkdir(self.root_path)
# download try: func = partial(download_file_id, dst_dir=dst_dir) with tqdm(total=len(file_ids), mininterval=5, file=tqdm_out) as pbar: for _ in tqdm(pool.imap_unordered(func, file_ids)): pbar.update() except KeyboardInterrupt: pass finally: pool.terminate() pool.join() if __name__ == '__main__': gauth = GoogleAuth() # method 1 - service account #scope = ['https://www.googleapis.com/auth/drive.file'] #gauth.credentials = SAC.from_json_keyfile_name('client_secret.json', scope) # method 2 - oauth auth_url = gauth.GetAuthUrl() print("Please visit\n\n{}\n\nto retrieve OAuth authorization code.".format( auth_url)) code = input(".. CODE: ") gauth.Auth(code) drive = GoogleDrive(gauth) download_link()
class CloudHandler: ''' ''' def __init__(self): self.DBConfig = AppConfig() self.FH = FileHandler() self.googleConfig = 'configs/gdrive.yaml' def ssl_seed(self,size=24, chars=string.ascii_uppercase + string.digits): self.randomString = ''.join(random.choice(chars) for _ in range(size)) return self def authenticategoogle(self): self.ga = GoogleAuth(self.googleConfig) try: self.ga.LoadClientConfig() self.ga.LoadCredentials() self.ga.Authorize() except: return self.ga.GetAuthUrl() #code = raw_input("code: ").strip() else: return True #auth loaded ok def googleauthorize(self,authCode): code = authCode.strip() self.ga.Auth(code) self.ga.SaveCredentials() return True def googleupload(self,filename): packageName = filename self.ga = GoogleAuth(self.googleConfig) self.ga.LoadClientConfig() self.ga.LoadCredentials() self.ga.Authorize() drive = GoogleDrive(self.ga) #flist = drive.ListFile({'q': "title contains '.crypt' and trashed = false"}) folderlistquery = drive.ListFile({'q': "title = 'SeccuDB' and mimeType = 'application/vnd.google-apps.folder' and trashed = false"}) cloudfolder = folderlistquery.GetList() if (len(cloudfolder) == 0): #create folder folder = drive.CreateFile() folder['title'] = "SeccuDB" folder['mimeType'] = "application/vnd.google-apps.folder" folder['parents'] = "root" folder.Upload() cloudfolder = folderlistquery.GetList() if (len(cloudfolder) == 0): print "error" raise error('GooglePermissionsError') cloudfolderid = cloudfolder[0]['id'] print cloudfolderid databaseListquery = drive.ListFile({'q': "'%s' in parents and trashed = false" % (cloudfolderid)}) databaseList = databaseListquery.GetList() database_file = drive.CreateFile() database_file['title'] = packageName database_file['parents']=[{"kind": "drive#fileLink" ,'id': str(cloudfolderid) }] #check if already exists, if so, get id and update databasenamelist = [] for databaseAvaliable in databaseList: databasenamelist.append(databaseAvaliable['title']) if packageName in databasenamelist: cloudPackageQuery = drive.ListFile({'q': "title = '%s' and trashed = false" % (packageName)}) cloudPackage = cloudPackageQuery.GetList() if(len(cloudPackage) > 1): # if theres more than one, go for the most recent packdates = [] for everypack in cloudPackage: packdates.append((everypack['modifiedByMeDate'],everypack['id'])) database_file['id'] = sorted(packdates,reverse=True)[0][1] else: database_file['id'] = cloudPackage[0]['id'] database_file.Upload() return True def getgooglefileid(self,title): print os.getcwd() self.ga = GoogleAuth(self.DBConfig.googleyaml) self.ga.LocalWebserverAuth() drive = GoogleDrive(self.ga) flist = drive.ListFile({'q': "title = '%s' and trashed = false"%title}) files = flist.GetList() if len(files)==0: return False else: return files[0]['id'] def simpleUploadGoogle(self,filename): #test_01_Files_Insert drive = GoogleDrive(self.ga) #f = drive.CreateFile({'fluidSecurity': parent_id}) file1 = drive.CreateFile() file1.SetContentFile(filename) # Read local file file1.Upload() # Upload it return True ##save file ID to config def simpleUpdateGoogle(self,filename,dbID): drive = GoogleDrive(self.ga) file1=drive.CreateFile({'id': dbID}) #overwrite by ID file1['title'] = filename file1.FetchContent() file1.SetContentFile(filename) file1.Upload() return True def simpleDownloadGoogle(self,filename): DeleteOldFile(self.filename) #delete local version, not that safe drive = GoogleDrive(self.ga) file1 = drive.CreateFile() file1['title'] = filename file1.FetchContent() # Force download and double check content return True # # # def ftpDown(filename,host,user,ftpass): # import ftplib # session = ftplib.FTP(host,user,ftpass) # file = open(filename, 'wb') # session.retrbinary('RETR %s' % filename, file.write) # file.close() # session.quit() # def uploadftp(self,filename,host,user,password): # upload logic import ftplib session = ftplib.FTP(host,user,password) file = open(filename,'rb') # file to send session.storbinary('STOR ' +filename, file) # send the file file.close() # close file and FTP session.quit() return True def authenticatedropbox(self): import dropbox # Get your app key and secret from the Dropbox developer website app_key = 'p2iu4n7f9yegl3u' app_secret = '0903whnau7p2zde' flow = dropbox.client.DropboxOAuth2FlowNoRedirect(app_key, app_secret) authorize_url = flow.start() print "============= authenticate dropbox" + str(authorize_url) pickled = pickle.dumps(flow) encodedPickle = base64.b64encode(pickled) self.DBConfig.putmap('cloudinfo','dropboxobject', str(encodedPickle) ) return authorize_url def uploaddropbox(self,filename,authcode): import socket socket.RAND_add(self.ssl_seed().randomString, 75.0) import dropbox try: # Get your app key and secret from the Dropbox developer website # encodedPickle =self.DBConfig.mapget('cloudinfo')['dropboxobject'] # decodedPickle = base64.b64decode(encodedPickle) # flow = pickle.loads(decodedPickle) app_key = 'p2iu4n7f9yegl3u' app_secret = '0903whnau7p2zde' flow = dropbox.client.DropboxOAuth2FlowNoRedirect(app_key, app_secret) authorize_url = flow.start() print "============= authenticate dropbox" + str(authorize_url) print "loaded ok" print "------ authcode: " + str(authcode) access_token, user_id = flow.finish(authcode) print "finidhed oko" client = dropbox.client.DropboxClient(access_token) print "finidhed client" print 'linked account: ', client.account_info() #Uploading files f = open(filename, 'rb') response = client.put_file('/'+str(filename), f) print "uploaded:", response except Exception as e: print("Upload failed: " + str(e)) return False return True def uploadgoogledrive(self,filename): googlefileid = self.getgooglefileid(filename) if googlefileid == False: self.simpleUploadGoogle(filename) else: self.simpleUpdateGoogle(filename,googlefileid) return True def uploadicloud(self,filename,email,password): return True def uploadskydrive(self,filename,email,password): return True
def test_01_LocalWebserverAuthWithClientConfigFromFile(self): # Test if authentication works with config read from file packageName = "test.z.crypt" foldername = "SeccuDB" ga = GoogleAuth('settings/test1.yaml') #ga.getAuthURL() try: ga.LoadClientConfig() ga.LoadCredentials() ga.Authorize() except: print ga.GetAuthUrl() code = raw_input("code: ").strip() ga.Auth(code) ga.SaveCredentials() print "complete Auth" drive = GoogleDrive(ga) #flist = drive.ListFile({'q': "title contains '.crypt' and trashed = false"}) folderlistquery = drive.ListFile({ 'q': "title = 'SeccuDB' and mimeType = 'application/vnd.google-apps.folder' and trashed = false" }) cloudfolder = folderlistquery.GetList() if (len(cloudfolder) == 0): #create folder folder = drive.CreateFile() folder['title'] = "SeccuDB" folder['mimeType'] = "application/vnd.google-apps.folder" folder['parents'] = "root" folder.Upload() cloudfolder = folderlistquery.GetList() if (len(cloudfolder) == 0): print "error" raise error('GooglePermissionsError') cloudfolderid = cloudfolder[0]['id'] print cloudfolderid databaseListquery = drive.ListFile( {'q': "'%s' in parents and trashed = false" % (cloudfolderid)}) databaseList = databaseListquery.GetList() database_file = drive.CreateFile() database_file['title'] = packageName database_file['parents'] = [{ "kind": "drive#fileLink", 'id': str(cloudfolderid) }] #check if already exists, if so, get id and update databasenamelist = [] for databaseAvaliable in databaseList: databasenamelist.append(databaseAvaliable['title']) if packageName in databasenamelist: cloudPackageQuery = drive.ListFile( {'q': "title = '%s' and trashed = false" % (packageName)}) cloudPackage = cloudPackageQuery.GetList() if (len(cloudPackage) > 1): # if theres more than one, go for the most recent packdates = [] for everypack in cloudPackage: packdates.append( (everypack['modifiedByMeDate'], everypack['id'])) database_file['id'] = sorted(packdates, reverse=True)[0][1] else: database_file['id'] = cloudPackage[0]['id'] database_file.Upload()
def settoken(server, info, code): gauth = GoogleAuth(DriveAPISecerts) gauth.LoadCredentialsFile(CredentialsFilePath) gauth.Auth(code) gauth.SaveCredentialsFile(CredentialsFilePath) return gauth
from pydrive.auth import GoogleAuth gauth = GoogleAuth() auth_url = gauth.GetAuthUrl() # Create authentication url user needs to visit code = AskUserToVisitLinkAndGiveCode( auth_url) # Your customized authentication flow gauth.Auth(code) # Authorize and build service from the code
class DriveHelper: def __init__(self): self.drive_auth_settings = constants.DRIVE_AUTH_SETTINGS self.drive_credentials_file = constants.DRIVE_CREDS_FILE self.auth = GoogleAuth(settings_file=self.drive_auth_settings) def get_authentication_url(self): return self.auth.GetAuthUrl() def set_authentication_code(self, code): is_auth = False try: self.auth.Auth(code) self.auth.SaveCredentialsFile(self.drive_credentials_file) is_auth = True except: print("Error occurred while trying to authenticate via given code: {0}".format(code)) finally: return is_auth def google_auth(self): self.auth.LoadCredentialsFile(self.drive_credentials_file) if self.auth.credentials is None: # Authenticate if credentials.json not there # auth.CommandLineAuth() # auth.LocalWebserverAuth() # custom_authentication_flow() print('Authenticate via this url: {0}'.format(self.auth.GetAuthUrl())) return None elif self.auth.access_token_expired: # Refresh them if expired self.auth.Refresh() else: # Initialize the saved creds self.auth.Authorize() return self.auth def upload_file(self, folder_id, file_name, file): is_uploaded = False if self.google_auth() is not None: try: drive = GoogleDrive(self.auth) mime = dict(title=file_name, parents=[{"kind": "drive#fileLink", "id": folder_id}]) _file = drive.CreateFile(mime) # Create GoogleDriveFile instance with title 'Hello.txt'. _file.SetContentFile(file) _file.Upload() is_uploaded = True except: print("Something ugly happened.. Couldn't upload file") finally: return is_uploaded print('Check to see if auth is none...') if self.google_auth() is not None: print("It's not..") drive = GoogleDrive(self.google_auth()) mime = dict(title="HelloWorld.txt", parents=[{"kind": "drive#fileLink", "id": constants.DRIVE_RAWDOCS_FOLDER_ID}]) _file = drive.CreateFile(mime) # Create GoogleDriveFile instance with title 'Hello.txt'. _file.SetContentString('Hello World!') # Set content of the file from given string. _file.Upload()
def pydrive_load(args): gauth = GoogleAuth() code = gauth.CommandLineAuth() if code != None: gauth.Auth(code) drive = GoogleDrive(gauth) files = GoogleDriveFile(gauth) # remove temp file for this share id pro_temp = get_project_temp(drive, files, args.driveid) # about = drive.GetAbout() # print(about) # get_root_info(files, DRIVE_ID) root_node = Node('root', data=path_info(id=DRIVE_ID, title='', parent='')) # drive_id = DRIVE_ID drive_id = args.driveid l = [] get_file_list(root_node, l, drive, drive_id) # list path tree if args.showtree: print('path tree is:') for pre, fill, node in RenderTree(root_node): print('{}{}'.format(pre, node.name)) # make dir base_dir = os.path.join(args.downdir, drive_id) mkdir_in_tree(base_dir, root_node) # list file if args.showlist: print('file list is:') current = 0 total = len(l) for i in l: if args.showlist: print( 'id: {}, is_folder: {}, title: {}, desc: {}, ext: {}, size: {}' .format(i.id, i.is_folder, i.title, i.desc, i.ext, i.size)) if len(i.parents) > 0: index = 0 for parent in i.parents: if args.showlist: print(' parents:{}={}, isRoot:{}'.format( index, parent['id'], parent['isRoot'])) index += 1 if args.showlist: print(' parent path={}'.format(i.parent_node.data.path)) retry = 0 if not i.is_folder: while retry < args.retry_count: try: print('# {}/{} begin!'.format(current, total)) try: file_path = i.parent_node.data.path file_title = i.title file_size = i.size file_id = i.id download_file(file_path, args.override, drive, file_id, file_title, file_size) except HttpError as http_error: if http_error.resp.status == 403 and str( http_error.content ).find('The download quota for this file has been exceeded' ) != -1: make_copy_and_download(file_path, drive.auth.service, args.override, drive, file_id, pro_temp, file_title, file_size) print_with_carriage_return('# {}/{} done!'.format( current, total)) break except Exception as e: retry += 1 print('unexpeted error={}, retry={}'.format(e, retry)) current += 1 # remove temp print('job done! remove project temp folder...') get_project_temp(drive, files, args.driveid, False)