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)
Пример #4
0
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)
Пример #5
0
    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()
Пример #6
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
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")
Пример #8
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())
Пример #9
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
Пример #10
0
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)
Пример #11
0
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
Пример #12
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")
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"
        ))
Пример #14
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 = {}
Пример #15
0
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()
Пример #16
0
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"

    # Get credentials and create an API client
    credentials = ServiceAccountCredentials.from_json_keyfile_name(client_secrets_file, scopes)