def getGdriveToken(request, response): creds = None if os.path.exists('token.pickle'): with open('token.pickle', 'rb') as token: creds = pickle.load(token) if not creds or not creds.valid: if creds and creds.expired and creds.refresh_token: creds.refresh(Request()) else: flow = InstalledAppFlow.from_client_secrets_file( config.getGdriveCredentialsFile(), SCOPES) creds = flow.run_local_server(port=0) with open('token.pickle', 'wb') as token: pickle.dump(creds, token) with open('gdrive.token', 'w') as token: token.write( json.dumps({ 'access_token': creds.token, 'refresh_token': creds.refresh_token })) r = {} r['access_token'] = creds.token r['refresh_token'] = creds.refresh_token with open(config.getGdriveCredentialsFile(), 'r') as f: r['credentials'] = json.loads(f.read()) if response is not None: response.write(json.dumps(r))
def downloadGdriveFile(response, url, start=None, end=None): creds = None if os.path.exists('token.pickle'): with open('token.pickle', 'rb') as token: creds = pickle.load(token) if not creds or not creds.valid: if creds and creds.expired and creds.refresh_token: creds.refresh(Request()) else: flow = InstalledAppFlow.from_client_secrets_file( config.getGdriveCredentialsFile(), SCOPES) creds = flow.run_local_server(port=0) with open('token.pickle', 'wb') as token: pickle.dump(creds, token) service = build('drive', 'v3', credentials=creds) info = getFileInfo(service, url) if not info: return server.Response404(None, response) return downloadProxyFile( 'https://www.googleapis.com/drive/v3/files/%s?alt=media' % info['id'], response, start, end, headers={'Authorization': 'Bearer ' + creds.token})
def listGdriveDir(path): r = {'dirs': [], 'files': []} creds = None if os.path.exists('token.pickle'): with open('token.pickle', 'rb') as token: creds = pickle.load(token) if not creds or not creds.valid: if creds and creds.expired and creds.refresh_token: creds.refresh(Request()) else: flow = InstalledAppFlow.from_client_secrets_file( config.getGdriveCredentialsFile(), SCOPES) creds = flow.run_local_server(port=0) with open('token.pickle', 'wb') as token: pickle.dump(creds, token) service = build('drive', 'v3', credentials=creds) bits = [x for x in path.replace('\\', '/').split('/') if x] if len(bits) == 0: r['dirs'].append({'name': 'mydrive'}) for item in gdriveDrives(service): r['dirs'].append({'name': item['name']}) else: teamDriveId = getTeamDriveId(service, path) for item in gdriveQuery(service, "'%s' in parents and trashed=false" % gdriveGetFolderId(service, path), teamDriveId=teamDriveId): o = {'name': item['name']} if 'size' in item: o['size'] = int(item['size']) if 'kind' in item: o['kind'] = item['kind'] if 'mimeType' in item and item['mimeType'] == \ 'application/vnd.google-apps.folder': r['dirs'].append(o) else: r['files'].append(o) return r
def on_gdrive(self): if config.getGdriveCredentialsFile() is None: webbrowser.open_new_tab( 'https://developers.google.com/drive/api/v3/quickstart/go', ) QMessageBox.information( self, 'Google Drive OAuth Setup', "You require a credentials.json file to set up Google Drive " + "OAuth. This file can be obtained from " + "https://developers.google.com/drive/api/v3/quickstart/go , " + "click on the blue button that says 'Enable the Drive API' " + "and save the credentials.json to t his application's " + "directory.", ) else: buttonReply = QMessageBox.question( self, 'Google Drive OAuth Setup', "Do you you want to setup GDrive OAuth?", QMessageBox.Yes | QMessageBox.No, QMessageBox.No, ) if buttonReply == QMessageBox.Yes: try: os.unlink('gdrive.token') # TODO: Remove bare except except: pass try: os.unlink('token.pickle') # TODO: Remove bare except except: pass server.controller.api.getGdriveToken(None, None) QMessageBox.information( self, 'Google Drive OAuth Setup', "OAuth has completed. Please copy gdrive.token and " + "credentials.json to your Nintendo Switch's " + "sdmc:/switch/tinfoil/ and/or sdmc:/switch/sx/ " + "directories.")
def test_get_gdrive_credentials_file_with_conf_credentials_file(self): self.fs.create_file('conf/credentials.json') self.assertIsNotNone(config.getGdriveCredentialsFile())
def test_get_gdrive_credentials_file_without_files(self): self.assertIsNone(config.getGdriveCredentialsFile())
def test_mapping_with_gdrive_credentials_file(self): self.assertIsNone(config.getGdriveCredentialsFile()) self.fs.create_file('credentials.json') self.assertEqual(config.paths.mapping()['gdrive'], '') self.assertEqual(config.paths.mapping()['.'], '.')