コード例 #1
0
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
コード例 #2
0
 def __init__(self,
              file_path,
              file_config="/etc/pydrivesync.yaml",
              gdriveIds=["root"]):
     self.download_folder = os.path.join(os.path.abspath(os.getcwd()),
                                         file_path, self.DRIVE_PATH)
     if not os.path.exists(self.download_folder):
         os.mkdir(self.download_folder)
     gauth = GoogleAuth(settings_file=file_config)
     gauth.CommandLineAuth()
     self.drive = GoogleDrive(gauth)
     self.gdriveIds = gdriveIds
     temp_file_md5_list = pkg_resources.resource_filename(
         __name__, "temp_file_md5_list.txt")
     if os.path.exists(temp_file_md5_list):
         with open(temp_file_md5_list, 'r') as f:
             l = f.read()
             if l is not None and l != "":
                 line = l.split(":")
                 self.current_list_file.append(line[0])
                 self.current_list_md5.append(line[1])
         f.close()
コード例 #3
0
    def __init__(self) -> None:

        # Create and authenticate the user
        self.auth = GoogleAuth(settings_file='config/settings.yaml')

        # Then create a new google drive instance and cache all of the files in the drive
        self.drive = GoogleDrive(self.auth)
        self.files = {file['id']: file for file in self.drive.ListFile({'q': "trashed=false"}).GetList()}
        self.trash = {file['id']: file for file in self.drive.ListFile({'q': "trashed=true"}).GetList()}

        # Finally we need to find the root of this google drive so we first need to get all parent references - root is
        # not considered a normal file
        all_parents = [parent for file in self.files.values() for parent in file['parents']]
        self.drive_root = [parent for parent in all_parents if parent['isRoot']].pop()
コード例 #4
0
    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)
コード例 #5
0
    def __init__(self):
        settings_yaml = rospy.get_param('~settings_yaml', None)
        self.share_type = rospy.get_param('~share_type', 'anyone')
        self.share_value = rospy.get_param('~share_value', 'anyone')
        self.share_role = rospy.get_param('~share_role', 'reader')
        self.share_with_link = rospy.get_param('~share_with_link', True)
        auth_max_trial = rospy.get_param('~auth_max_trial', -1)
        auth_wait_seconds = rospy.get_param('~auth_wait_seconds', 10.0)
        if settings_yaml is not None:
            self.gauth = GoogleAuth(settings_yaml)
        else:
            rospy.logerr('param: ~settings_yaml is not correctly set.')
            sys.exit(1)

        rospy.loginfo('Google drive authentication starts.')
        auth_success = False
        auth_count = 0
        while (not auth_success
               and (auth_max_trial < 0 or auth_count < auth_max_trial)):
            try:
                self.gauth.LocalWebserverAuth()
                auth_success = True
            except ServerNotFoundError as e:
                rospy.logerr('Authentication failed: {}'.format(e))
                auth_count = auth_count + 1
                time.sleep(auth_wait_seconds)
        if not auth_success:
            rospy.logerr(
                'Authentication failed {} times.'.format(auth_max_trial))
            sys.exit(1)
        self.gdrive = GoogleDrive(self.gauth)
        rospy.loginfo('Google drive authentication finished.')
        self.upload_server = rospy.Service('~upload', Upload, self._upload_cb)
        self.upload_multi_server = rospy.Service('~upload_multi',
                                                 MultipleUpload,
                                                 self._upload_multi_cb)
        rospy.loginfo('Finish initialization, Server started.')
コード例 #6
0
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())
コード例 #7
0
ファイル: gDrive.py プロジェクト: thetommycox/AeroGroupMeBot
    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()
コード例 #8
0
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")
コード例 #9
0
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
コード例 #10
0
ファイル: test_oauth.py プロジェクト: iterative/PyDrive2
    def test_07_ServiceAuthFromSavedCredentialsJsonFile(self):
        # Have an initial auth so that credentials/7.dat gets saved
        ga = GoogleAuth(settings_file_path("test_oauth_test_07.yaml"))
        ga.ServiceAuth()
        self.assertTrue(os.path.exists(ga.settings["save_credentials_file"]))

        # Secondary auth should be made only using the previously saved
        # login info
        ga = GoogleAuth(settings_file_path("test_oauth_test_07.yaml"))
        ga.ServiceAuth()

        self.assertEqual(ga.access_token_expired, False)
        time.sleep(1)
コード例 #11
0
    def __authenticate(self):
        credentials_full_path = os.path.join(self._credentials_path,
                                             'credentials.json')
        token = os.path.join(self._credentials_path, 'credentials')

        gauth = GoogleAuth()
        gauth.LoadClientConfigFile(credentials_full_path)

        if os.path.exists(token):
            gauth.LoadCredentialsFile(token)

        cred = gauth.credentials
        if not cred or cred.invalid:
            if cred and cred.access_token_expired and cred.refresh_token:
                gauth.Refresh()
            else:
                gauth.LocalWebserverAuth()
            gauth.SaveCredentialsFile(token)
        return gauth
コード例 #12
0
ファイル: gdrive.py プロジェクト: wishx97/weibo-crawler
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)
コード例 #13
0
def google_drive_connect(id, path, auto_auth=True):
    '''
    Method for connecting to the subject's Google Drive
    '''
    # Initialize Google Auth object
    try:
        gauth = GoogleAuth(settings_file=path + '/Subjects/' + id +
                           '/pydrive.yaml')
        local_set = True
    except:
        gauth = GoogleAuth()
        local_set = False
    # String to the credentials file
    credPath = path + '/Subjects/' + id + "/mycreds.txt"
    # Try to load saved client credentials
    gauth.LoadCredentialsFile(credPath)
    # If this credential does not exist
    if gauth.credentials is None:
        # Authenticate if they're not there
        raise Exception(
            "Error: the credentials for this subject do not exist.")
    elif gauth.access_token_expired:
        # Refresh them if expired
        print("Note: This subject's credentials are being renewed.")
        # try to refresh the access token
        try:
            gauth.Refresh()
        # may fail if the refresh token does not exist
        except:
            #gauth.LocalWebserverAuth()
            #gauth.Authenticate()
            print("ERROR: Authorization refresh failed!")
            return None
    #print("Gauth:")
    #print(gauth)
    # Save the current credentials to the file
    #gauth.SaveCredentialsFile(credPath)
    # Initialize the Google Drive Connection
    drive = GoogleDrive(gauth)
    #print("Drive Object:")
    #print(drive)
    # Return the Google Drive object
    return drive
コード例 #14
0
# pydrive2 is an actively maintained fork
from pydrive2.drive import GoogleDrive
from pydrive2.auth import GoogleAuth, ServiceAccountCredentials

gauth = GoogleAuth()
# drive is nice for sanity checking, but should use drive.file for the Real Thing
# https://developers.google.com/drive/api/v3/about-auth
scope = ['https://www.googleapis.com/auth/drive']
gauth.credentials = ServiceAccountCredentials.from_json_keyfile_name(
    'google-credentials.json', scope)
drive = GoogleDrive(gauth)

for file_list in drive.ListFile({'maxResults': 10}):
    print('Received {} files from Files.list()'.format(
        len(file_list)))  # <= 10
    for file1 in file_list:
        print('title: {}, id: {}, mime: {}'.format(file1['title'], file1['id'],
                                                   file1['mimeType']))
コード例 #15
0
ファイル: views.py プロジェクト: MarlNox/datafindhub
from .tasks import upload_via_celery

from pydrive2.auth import GoogleAuth, AuthenticationError
from pydrive2.drive import GoogleDrive
import pandas as pd
import json
from django.utils import timezone
from .models import FileModel
from django.core.files import File
# Create your views here.
import os

input_path = settings.MEDIA_ROOT + '/screenshots/full/'
output_path = settings.MEDIA_ROOT + '/ocr/'

gauth = GoogleAuth()


@csrf_exempt
def login(request):
    return HttpResponseRedirect(gauth.GetAuthUrl())


def authorized_view(request):
    x = request.GET["code"]
    request.session["code"] = x

    gauth.Auth(x)

    cred_file = re.sub('[\W_]+', '', "file_{}".format(str(
        timezone.now()))) + ".txt"
コード例 #16
0
ファイル: test_oauth.py プロジェクト: wruef/PyDrive2
 def CheckCredentialsFile(self, credentials, no_file=False):
   ga = GoogleAuth('pydrive2/test/settings/test_oauth_default.yaml')
   ga.LoadCredentialsFile(credentials)
   self.assertEqual(ga.access_token_expired, no_file)
コード例 #17
0
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
コード例 #18
0
class GoogledrivebackupPlugin(octoprint.plugin.SettingsPlugin,
                              octoprint.plugin.AssetPlugin,
                              octoprint.plugin.TemplatePlugin,
                              octoprint.plugin.EventHandlerPlugin,
                              octoprint.plugin.SimpleApiPlugin):

    ##~~ SettingsPlugin mixin

    def __init__(self):
        self.gauth = None

    def get_settings_defaults(self):
        return dict(
            cert_saved=False,
            cert_authorized=False,
            installed_version=self._plugin_version,
        )

    ##~~ SimpleApiPlugin mixin

    def get_api_commands(self):
        return dict(gen_secret=["json_data"], authorize=["auth_code"])

    def on_api_command(self, command, data):
        from octoprint.server import user_permission
        import flask
        if not user_permission.can():
            return flask.make_response("Insufficient rights", 403)

        from pydrive2.auth import GoogleAuth
        config_file = "{}/client_secrets.json".format(
            self.get_plugin_data_folder())
        credentials_file = "{}/credentials.json".format(
            self.get_plugin_data_folder())
        if not self.gauth:
            self.gauth = GoogleAuth()

        if command == "gen_secret":
            import json
            # write out our client_secrets.json file
            with open(config_file, "w") as f:
                f.write(json.dumps(data["json_data"]))
            self._settings.set(["cert_saved"], True)
            self._settings.save()

            self.gauth.LoadClientConfigFile(config_file)
            self.gauth.GetFlow()
            self.gauth.flow.params.update({'access_type': 'offline'})
            self.gauth.flow.params.update({'approval_prompt': 'force'})
            auth_url = self.gauth.GetAuthUrl()
            return flask.jsonify(dict(cert_saved=True, url=auth_url))

        if command == "authorize":
            self._logger.info("Attempting to authorize Google App")
            if not self.gauth:
                return flask.jsonify(dict(authorized=False))
            # Try to load saved client credentials
            self.gauth.Auth(data["auth_code"])
            self.gauth.SaveCredentialsFile(credentials_file)
            self._settings.set(["cert_authorized"], True)
            self._settings.save()
            return flask.jsonify(dict(authorized=True))

        ##~~ AssetPlugin mixin

    def get_assets(self):
        # Define your plugin's asset files to automatically include in the
        # core UI here.
        return dict(js=["js/googledrivebackup.js"])

    ##~~ EventHandlerPlugin mixin

    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

        ##~~ Softwareupdate hook

    def get_update_information(self):
        return dict(googledrivebackup=dict(
            displayName="Google Drive Backup",
            displayVersion=self._plugin_version,

            # version check: github repository
            type="github_release",
            user="******",
            repo="OctoPrint-GoogleDriveBackup",
            current=self._plugin_version,
            stable_branch=dict(
                name="Stable", branch="master", comittish=["master"]),
            prerelease_branches=[
                dict(name="Release Candidate",
                     branch="rc",
                     comittish=["rc", "master"])
            ],
            # update method: pip
            pip=
            "https://github.com/jneilliii/OctoPrint-GoogleDriveBackup/archive/{target_version}.zip"
        ))
コード例 #19
0
from pydrive2.auth import GoogleAuth
from pydrive2.drive import GoogleDrive

# Authenticate the client.
gauth = GoogleAuth()
gauth.LocalWebserverAuth()
drive = GoogleDrive(gauth)

# Create a file, set content, and upload.
file1 = drive.CreateFile()
original_file_content = "Generic, non-exhaustive\n ASCII test string."
file1.SetContentString(original_file_content)
# {'convert': True} triggers conversion to a Google Drive document.
file1.Upload({"convert": True})


# Download the file.
file2 = drive.CreateFile({"id": file1["id"]})

# Print content before download.
print("Original text:")
print(bytes(original_file_content.encode("unicode-escape")))
print("Number of chars: %d" % len(original_file_content))
print("")
#     Original text:
#     Generic, non-exhaustive\n ASCII test string.
#     Number of chars: 43


# Download document as text file WITH the BOM and print the contents.
content_with_bom = file2.GetContentString(mimetype="text/plain")
コード例 #20
0
ファイル: gdrive.py プロジェクト: woodshop/dvc
    def drive(self):
        from pydrive2.auth import RefreshError

        if not hasattr(self, "_gdrive"):
            from pydrive2.auth import GoogleAuth
            from pydrive2.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 pydrive2.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.remote_root_id = self.get_remote_id(
                self.path_info, create=True
            )
            self._cached_dirs, self._cached_ids = self.cache_root_dirs()

        return self._gdrive
コード例 #21
0
ファイル: sheet.py プロジェクト: shcheklein/Gdrive_folders
class Sheet():
    def __init__(self,
                 folder_inf=FOLDER_INFORME_ID,
                 folder_rev=FOLDER_REVELAMIENTO_ID,
                 sheet_id=SHEET_ID,
                 remito=REMITO_COL,
                 informe=INFORME_COL,
                 revelamiento=REVELEMIENTO_COL) -> None:

        #Inicicializando Variables de Settings
        self.FOLDER_INFORME_ID = folder_inf
        self.FOLDER_REVELAMIENTO_ID = folder_rev
        self.SHEET_ID = sheet_id
        self.REMITO_COL = remito
        self.INFORME_COL = informe
        self.REVELEMIENTO_COL = revelamiento

        #* Variables para guardar en DB

        self.numero_estudio_col = NUMERO_ESTUDIO_COL
        self.id_cliente_col = ID_CLIENTE_COL
        self.nombre_cliente_col = NOMBRE_CLIENTE_COL
        self.tipo_estudio_col = TIPO_ESTUDIO_COL
        self.aprob2_col = APROB2_COL

        #* Variables para envio de correo

        self.sheet_email_id = SHEET_EMAIL_ID
        self.id_cliente_email_col = ID_CLIENTE_EMAIL_COL
        self.correo_cliente_col = CORREO_CLIENTE_COL

        # Declarando Variables de trabajo

        self.informes = []
        self.revelamientos = []
        self.sh = []
        self.ws = []

        #? Variables de actualizar informe

        self.col_remito = []
        self.col_informe = []
        self.col_revelamiento = []

        #? Variables de guardar en DataBase

        self.numero_estudio = []
        self.id_cliente = []
        self.nombre_cliente = []
        self.tipo_estudio = []
        self.aprob2 = []

        self.id_cliente_email = []
        self.correo_cliente = []

        # Autenticacion

        self.gauth = GoogleAuth()
        self.gc = pygsheets.authorize(client_secret='sheet_secret.json')
        self.gauth.CommandLineAuth()

    # Obtener valores de trabajos actuales
    def get_values(self):

        self.informes = self.search_folder(self.FOLDER_INFORME_ID)
        self.revelamientos = self.search_folder(self.FOLDER_REVELAMIENTO_ID)
        self.sh = self.gc.open_by_key(self.SHEET_ID)
        self.ws = self.sh.sheet1

        self.col_remito = self.ws.get_col(REMITO_COL)
        self.col_informe = self.ws.get_col(INFORME_COL, value_render="FORMULA")
        self.col_revelamiento = self.ws.get_col(REVELEMIENTO_COL,
                                                value_render="FORMULA")

    # Requiere ID de una carpeta, retorna lista de archivos y/o sub carpetas
    def search_folder(self, folderid: str):

        drive = GoogleDrive(self.gauth)
        file_list = drive.ListFile({
            'q':
            f"'{folderid}' in parents and trashed=false"
        }).GetList()
        return file_list

    # Iterar por cada uno de los informes, revisar en la columna remito y escribir los hiperlinks
    # No retorna nada
    def escribir_informes(self, folder_list, col_objetivo, col_lista):

        for folder_element in folder_list:
            element = folder_element['title'].split('.', 1)
            if element[0] in self.col_remito:
                index = self.col_remito.index(element[0])
                if not col_lista[index]:
                    link = folder_element["alternateLink"]
                    title = folder_element["title"]
                    col_lista[index] = f'=HYPERLINK("{link}";"{title}")'

        self.ws.update_col(col_objetivo, col_lista)

    # Obtiene los valores de todas las variables de interes de la base de datos

    def get_status_columns(self):
        self.sh = self.gc.open_by_key(self.SHEET_ID)
        self.ws = self.sh.sheet1

        self.numero_estudio = self.ws.get_col(self.numero_estudio_col)
        self.id_cliente = self.ws.get_col(self.id_cliente_col)
        self.nombre_cliente = self.ws.get_col(self.nombre_cliente_col)
        self.tipo_estudio = self.ws.get_col(self.tipo_estudio_col)
        self.aprob2 = self.ws.get_col(self.aprob2_col)

    def get_email_inf(self):

        self.sh = self.gc.open_by_key(self.sheet_email_id)
        self.ws = self.sh.sheet1
        self.id_cliente_email = self.ws.get_col(self.id_cliente_col)
        self.correo_cliente = self.ws.get_col(self.correo_cliente_col)
コード例 #22
0
    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 = {}
コード例 #23
0
from pydrive2.auth import GoogleAuth
from pydrive2.drive import GoogleDrive
import os
import sys
import zipfile
from datetime import date

dir_name = '/appdata'

f = open("/REMAIN.txt", "r")
REMAIN = int(f.read()) - 1

f = open("/TEAM_ID.txt", "r")
TEAM_ID = str(f.read()).rstrip()

gauth = GoogleAuth()

gauth.LoadCredentialsFile("/credentials/mycreds.txt")
if gauth.access_token_expired:
    gauth.Refresh()
else:
    gauth.Authorize()

gauth.SaveCredentialsFile("/credentials/mycreds.txt")

drive = GoogleDrive(gauth)


def retrieve_file_paths(dirName):
    file_paths = []
    for root, directories, files in os.walk(dirName):
コード例 #24
0
 def Press_Authenticate(self, instance):
     gauth = GoogleAuth()
     gauth.LocalWebserverAuth()
     GDrive = GoogleDrive(gauth)  # Google Drive Class Object
     return
コード例 #25
0
from pydrive2.auth import GoogleAuth

if __name__ == "__main__":
    try:
        gauth = GoogleAuth()
        gauth.LoadCredentialsFile("credentials.json")
        if self.gauth.credentials is None:
            gauth.LocalWebserverAuth()
            gauth.SaveCredentialsFile("credentials.json")
            print("Credentials.json has been created")
        else:
            print("A valid credentials.json already exists")
    except:
        print("\nPlease make sure to run this file close to settings.yaml")
コード例 #26
0
ファイル: bot.py プロジェクト: alexacallmebaka/OpenMediaBot
    def __init__(self, configfile = None,  **kwargs):

        #These are the default values for our Bot.
        defaults = {"name":"OpenMediaBot","db":"media.db","gdrive_settings":"settings.yaml"}
        
        #Update the attributes defaults dictionary with any kwargs provided by the user.
        #Since a dictionary does not allow duplicate keys, kwargs provided by the user that were previously set in the default dict will override thier default values.
        defaults.update(kwargs)

        self.__dict__.update(defaults)

        #If a configfile is provided, we will use the values in this file to override any default values or kwargs provided in the attributes dict.
        if configfile is not None:
            
            with open(configfile) as jsonfile:
                
                import json
                
                self.__dict__.update(json.load(jsonfile))

        #We use the bot name as the name for our SQL table, this allows us to house multiple bots in the same databse file.
        #We have to  make sure we sanitize th name of the bot in order to prevent SQL inquection attacks.
        if re.search("[\";\+=()\']", self.name) is not None:
            raise ValueError("The characters [\";\+=()\'] are not allowed in bot names, as they leave you vulnerable to SQL injection attacks.")
        #Set up the bot's logger

        #Create a logger with the bot's name, and set the level to INFO.
        self.logger = logging.getLogger(self.name)
        self.logger.setLevel(logging.INFO)

        #Format our logs.
        formatter = logging.Formatter("%(name)s:%(asctime)s:%(levelname)s:%(message)s")

        #Create a stream handler which logs to the console.
        stream_handler = logging.StreamHandler()

        stream_handler.setFormatter(formatter)

        self.logger.addHandler(stream_handler)

        #If a path is supplied, create the specified filehandler to print to the log file.
        if self.__dict__.get("logpath") is not None:

            file_handler = logging.FileHandler(filename=self.logpath)

            file_handler.setFormatter(formatter)

            self.logger.addHandler(file_handler)

        #If Google Drive folders are provided, create an autheticated PyDrive object.
        if self.__dict__.get("drive_folders") is not None:

            self.logger.info("Connecting to Google Drive...")
            
            #Used for Google Drive.
            from pydrive2.auth import GoogleAuth
            from pydrive2.drive import GoogleDrive

            self.drive = GoogleDrive(GoogleAuth(settings_file=self.gdrive_settings))

        #Connect to our SQLite DB.
        self.connection = sqlite3.connect(self.db)

        #Create a cursor to execute commands within the database.
        self.cursor = self.connection.cursor()
コード例 #27
0
 def __init__(self):
     try:
         self.auth = GoogleAuth(settings_file=SETTINGS_YAML)
     except NameError as error:
         log.error(error)
         self.auth = None
コード例 #28
0
ファイル: test_oauth.py プロジェクト: wruef/PyDrive2
 def test_05_ConfigFromSettingsWithoutOauthScope(self):
   # Test if authentication works without oauth_scope
   ga = GoogleAuth('pydrive2/test/settings/test_oauth_test_05.yaml')
   ga.LocalWebserverAuth()
   self.assertEqual(ga.access_token_expired, False)
   time.sleep(1)
コード例 #29
0
ファイル: gdrive.py プロジェクト: vijay120/dvc
    def _drive(self):
        from pydrive2.auth import GoogleAuth
        from pydrive2.drive import GoogleDrive

        if os.getenv(GDriveTree.GDRIVE_CREDENTIALS_DATA):
            with open(self._gdrive_user_credentials_path, "w") as cred_file:
                cred_file.write(os.getenv(GDriveTree.GDRIVE_CREDENTIALS_DATA))

        auth_settings = {
            "client_config_backend": "settings",
            "save_credentials": True,
            "save_credentials_backend": "file",
            "save_credentials_file": self._gdrive_user_credentials_path,
            "get_refresh_token": True,
            "oauth_scope": [
                "https://www.googleapis.com/auth/drive",
                "https://www.googleapis.com/auth/drive.appdata",
            ],
        }

        if self._use_service_account:
            auth_settings["service_config"] = {
                "client_service_email": self._service_account_email,
                "client_user_email": self._service_account_user_email,
                "client_pkcs12_file_path": self._service_account_p12_file_path,
            }
        else:
            auth_settings["client_config"] = {
                "client_id": self._client_id or self.DEFAULT_GDRIVE_CLIENT_ID,
                "client_secret": self._client_secret
                or self.DEFAULT_GDRIVE_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.update(auth_settings)

        # Pass non existent settings path to force DEFAULT_SETTINGS loadings
        gauth = GoogleAuth(settings_file="")

        try:
            logger.debug(
                "GDrive remote auth with config '{}'.".format(
                    GoogleAuth.DEFAULT_SETTINGS
                )
            )
            if self._use_service_account:
                gauth.ServiceAuth()
            else:
                gauth.CommandLineAuth()
                GDriveTree._validate_credentials(gauth, auth_settings)

        # Handle AuthenticationError, RefreshError and other auth failures
        # It's hard to come up with a narrow exception, since PyDrive throws
        # a lot of different errors - broken credentials file, refresh token
        # expired, flow failed, etc.
        except Exception as exc:
            raise GDriveAuthError(self.credentials_location) from exc
        finally:
            if os.getenv(GDriveTree.GDRIVE_CREDENTIALS_DATA):
                os.remove(self._gdrive_user_credentials_path)

        return GoogleDrive(gauth)
コード例 #30
0
ファイル: test_oauth.py プロジェクト: wruef/PyDrive2
 def test_06_ServiceAuthFromSavedCredentialsFile(self):
   setup_credentials("credentials/6.dat")
   ga = GoogleAuth('pydrive2/test/settings/test_oauth_test_06.yaml')
   ga.ServiceAuth()
   self.assertEqual(ga.access_token_expired, False)
   time.sleep(1)