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
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 __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)
def __init__(self): ##vars to use later self.lastScheduleUpdateTime = datetime.datetime.min self.scheduleData = [] ##load important stuff self.root = LOAD_ENV_VARS.ENV_VARS['root'] self.access_token = LOAD_ENV_VARS.ENV_VARS['gd_access_token'] self.client_secret = LOAD_ENV_VARS.ENV_VARS['gd_client_secret'] self.client_id = LOAD_ENV_VARS.ENV_VARS['gd_client_id'] self.refresh_token = LOAD_ENV_VARS.ENV_VARS['gd_refresh_token'] self.token_expiry = LOAD_ENV_VARS.ENV_VARS['gd_token_expiry'] if True: ##make client creds Text = """{"access_token": %s, "client_id": %s, "client_secret": %s, "refresh_token": %s, "token_expiry": %s, "token_uri": "https://oauth2.googleapis.com/token", "user_agent": null, "revoke_uri": "https://oauth2.googleapis.com/revoke", "id_token": null, "id_token_jwt": null, "token_response": {"access_token": %s, "expires_in": 3600, "refresh_token": %s, "scope": "https://www.googleapis.com/auth/drive", "token_type": "Bearer"}, "scopes": ["https://www.googleapis.com/auth/drive"], "token_info_uri": "https://oauth2.googleapis.com/tokeninfo", "invalid": false, "_class": "OAuth2Credentials", "_module": "oauth2client.client"}""" % ( self.access_token, self.client_id, self.client_secret, self.refresh_token, self.token_expiry, self.access_token, self.refresh_token) f = open("mycreds.txt", "w+") f.write(Text) f.close() ##make client secrets from enviromental variables Text = '''{"installed":{"client_id":%s,"project_id":"quickstart-1564436220867","auth_uri":"https://accounts.google.com/o/oauth2/auth","token_uri":"https://oauth2.googleapis.com/token","auth_provider_x509_cert_url":"https://www.googleapis.com/oauth2/v1/certs","client_secret":%s,"redirect_uris":["urn:ietf:wg:oauth:2.0:oob","http://localhost"]}}''' % ( self.client_id, self.client_secret) f = open("client_secrets.json", "w+") f.write(Text) f.close() else: self.root = 'Python Bot Test' # Try to load saved client credentials gauth = GoogleAuth() gauth.LoadCredentialsFile("mycreds.txt") if gauth.credentials is None: # Authenticate if they're not there 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("mycreds.txt") self.UpdateEnvVars() #initialize drive object self.drive = GoogleDrive(gauth) ##Setup sechdule self.loadSchedule()
def login(): GoogleAuth.DEFAULT_SETTINGS['client_config_file'] = credencials_directory gauth = GoogleAuth() gauth.LoadCredentialsFile(credencials_directory) if gauth.credentials is None: gauth.LocalWebserverAuth(port_numbers=[8092]) elif gauth.access_token_expired: gauth.Refresh() else: gauth.Authorize() gauth.SaveCredentialsFile(credencials_directory) credentials = GoogleDrive(gauth) return credentials
def main(): gauth = GoogleAuth() # Try to load saved client credentials gauth.LoadCredentialsFile("secret.json") if gauth.credentials is None: # Authenticate if they're not there gauth.LocalWebserverAuth() elif gauth.access_token_expired: # Refresh them if expired gauth.Refresh() else: # Initialize the saved credentials gauth.Authorize() # Save the current credentials to a file gauth.SaveCredentialsFile("secret.json")
def Upload_To_GDrive(): global File_Name gauth = GoogleAuth() gauth.LoadCredentialsFile("mycreds.txt") if gauth.credentials is None: gauth.GetFlow() gauth.flow.params.update({'access_type': 'offline'}) gauth.flow.params.update({'approval_prompt': 'force'}) gauth.LocalWebserverAuth() elif gauth.access_token_expired: try: gauth.Refresh() except Exception as e: print(e) else: gauth.Authorize() gauth.SaveCredentialsFile("mycreds.txt") drive = GoogleDrive(gauth) folder_id = [ 'Folder_String_A', 'Folder_String_B', 'Folder_String_C', 'Folder_String_D' ] for k in range(len(File_Name)): try: f = drive.CreateFile({ 'title': f'{File_Name[k]}.csv', "parents": [{ "kind": "drive#fileLink", "id": folder_id[k] }] }) f.SetContentFile(f'./Data/{File_Name[k]}.csv') f.Upload() print("Uploading succeeded!") except: print("Uploading failed.") print(time.ctime())
def initial_gdrive(): ''' Authorize and refresh Google Drive token. Reference: https://stackoverflow.com/a/24542604/10114014 ''' gauth = GoogleAuth() # Try to load saved client credentials gauth.LoadCredentialsFile("mycreds.txt") if gauth.credentials is None: # Authenticate if they're not there 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("mycreds.txt") return GoogleDriveWithBytes(gauth)
from pydrive2.auth import GoogleAuth #potrebna knjiđnica PyDrive2, ki se jo dobi z pip install pydrive2 from pydrive2.drive import GoogleDrive gauth = GoogleAuth() #Potrebno je imeti na kompu ali client_secrets.json ali pa mycreds.txt, ki ne pridejo s knjižnco zraven # Try to load saved client credentials gauth.LoadCredentialsFile("mycreds.txt") if gauth.credentials is None: # Authenticate if they're not there 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("mycreds.txt") drive = GoogleDrive(gauth) ##FUNKCIJE def delete_file(file_id): file1 = drive.CreateFile({'parents': [{'id': '1eeA-y9DjF4zOuH4z5wkOB5nSehRtGneV'}],'id': file_id}) file1.Delete() def create_file(name,content): file1 = drive.CreateFile({'title': name,'parents': [{'id': '1eeA-y9DjF4zOuH4z5wkOB5nSehRtGneV'}]}) # Create GoogleDriveFile instance with title 'Hello.txt'. file1.SetContentString(content,encoding="utf-8") file1.Upload() print("File was created under the id %s" %(file1['id'])) return file1
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 = {}
class Pinguin(QMainWindow): login_signal = pyqtSignal() def __init__(self): super().__init__() self.db = PinguinDB() self.trello = Trello() self.auth = None self.google_client = GoogleClient() self.login_signal.connect(self.login_success) self.login_menu = Login_Window(self.login_signal, self.db, self.trello) self.main_window = Main_Menu(self.db, self.trello, self.google_client) #self.mutex = QMutex() @pyqtSlot() def login_success(self): self.main_window.show() self.login_menu.close() if self.trello.client == None: print("setting up trello client") print(self.db.user.user_id) self.trello.action_setup2(self.db.user.user_id) print("trello client all set") print("setting up google auth") #print(self.main_window.ui.google_client) self.auth = GoogleAuth() #print(auth) # Try to load saved client credentials self.auth.LoadCredentialsFile("Credentials.json") if self.auth.credentials is None: print("no creds") # Authenticate if they're not there self.auth.LocalWebserverAuth() elif self.auth.access_token_expired: print("creds expired") # Refresh them if expired self.auth.LocalWebserverAuth() #self.auth.Refresh() print("creds renewed") else: print("new creds") # Initialize the saved creds self.auth.Authorize() # Save the current credentials to a file #self.auth.SaveCredentialsFile("Credentials.json") print("authorization complete") self.google_client.set_client(self.auth) self.main_window.ui.google_client = self.google_client #print(self.main_window.ui.google_client) #self.main_window.ui.set_google_client(GoogleClient(self.auth)) #self.main_window.show() print("heresdasd") self.main_window.ui.widgets_refresh() self.main_window.ui.widgets_timer.start(30000) def run(self): self.login_menu.show()
class Drive(object): def __init__(self): self.logger = logging.getLogger(__name__) self.login() def login(self): self.gauth = GoogleAuth() self.gauth.LoadCredentialsFile("mycreds.txt") self.logger.debug("Loading credentials") if self.gauth.credentials is None: self.logger.debug("First authentication") # Authenticate if they're not there self.gauth.LocalWebserverAuth() elif self.gauth.access_token_expired: self.logger.debug("Token has expired, refreshing") # Refresh them if expired self.gauth.Refresh() else: self.logger.debug("Connected") # Initialize the saved creds self.gauth.Authorize() # Save the current credentials to a file self.gauth.SaveCredentialsFile("mycreds.txt") # Create GoogleDrive instance with authenticated GoogleAuth instance self.drive = GoogleDrive(self.gauth) def find_folders(self, folder_name): file_list = self.drive.ListFile({ "q": "title='{}' and mimeType contains 'application/vnd.google-apps.folder' and trashed=false" .format(folder_name) }).GetList() return file_list def create_subfolder(self, folder, sub_folder_name): new_folder = self.drive.CreateFile({ "title": "{}".format(sub_folder_name), "mimeType": "application/vnd.google-apps.folder", }) if folder is not None: new_folder["parents"] = [{u"id": folder["id"]}] new_folder.Upload() self.logger.debug("Folder created {}/{}".format( folder, sub_folder_name)) return new_folder def upload_files_to_folder(self, fnames, folder): for fname in fnames: nfile = self.drive.CreateFile({ "title": os.path.basename(fname), "parents": [{ u"id": folder["id"] }] }) nfile.SetContentFile(fname) nfile.Upload()
id ='*****@*****.**' key = base64.b64decode(open("configs\private_key.json", "r").read()) live_3d = False # creds = Credentials.from_json_keyfile_name('client_secrets.json', scopes) # service = build('drive', 'v2', credentials=creds) g_login = GoogleAuth() #g_login.creds = creds g_login.LoadCredentialsFile('configs\mycreds.txt') if g_login.credentials is None: g_login.LocalWebserverAuth() elif g_login.access_token_expired: g_login.Refresh() else: g_login.Authorize() g_login.SaveCredentialsFile('configs\mycreds.txt') drive = GoogleDrive(g_login) folder_id = '1eAnQD9mV0xhqaMVoeSGA06NrU1R6ik7Y' #====polling loop for youtube; update manually for now.==== def poll_loop(channel_id): # Disable OAuthlib's HTTPS verification when running locally. # *DO NOT* leave this option enabled in production. os.environ["OAUTHLIB_INSECURE_TRANSPORT"] = "1" api_service_name = "youtube" api_version = "v3" client_secrets_file = "configs\client_secrets.json"