コード例 #1
0
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))
コード例 #2
0
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(request, response)

    #request = service.files().get_media(fileId=info['id'])

    return downloadProxyFile(
        'https://www.googleapis.com/drive/v3/files/%s?alt=media' % info['id'],
        response,
        start,
        end,
        headers={'Authorization': 'Bearer ' + creds.token})
コード例 #3
0
ファイル: server.py プロジェクト: jasii/nut
    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')
                except:
                    pass

                try:
                    os.unlink('token.pickle')
                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."
                )
コード例 #4
0
ファイル: gdrive.py プロジェクト: jeffangama/nut
    def chunk(self, callback, offset=None, size=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, self.url)

            if not info:
                return server.Response404(None, response)

            download('https://www.googleapis.com/drive/v3/files/%s?alt=media' %
                     info['id'],
                     callback,
                     offset,
                     None if offset is None else (offset + size - 1),
                     headers={'Authorization': 'Bearer ' + creds.token})
コード例 #5
0
ファイル: gdrive.py プロジェクト: jeffangama/nut
    def ls(self):
        entries = []
        r = {'dirs': [], 'files': []}
        path = self.url[len('gdrive:/'):]

        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):
                entries.append(
                    Fs.driver.DirEntry(Fs.driver.join(self.url, 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':
                    entries.append(
                        Fs.driver.DirEntry(Fs.driver.join(self.url,
                                                          o['name'])))
                else:
                    entries.append(
                        Fs.driver.FileEntry(
                            Fs.driver.join(self.url, o['name']), o['size']))

        return entries
コード例 #6
0
ファイル: nut_config_test.py プロジェクト: gitter-badger/nut
 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()['.'], '.')
コード例 #7
0
ファイル: nut_config_test.py プロジェクト: gitter-badger/nut
 def test_get_gdrive_credentials_file_with_conf_credentials_file(self):
     self.fs.create_file('conf/credentials.json')
     self.assertIsNotNone(Config.getGdriveCredentialsFile())
コード例 #8
0
ファイル: nut_config_test.py プロジェクト: gitter-badger/nut
 def test_get_gdrive_credentials_file_without_files(self):
     self.assertIsNone(Config.getGdriveCredentialsFile())