Esempio n. 1
0
def get_google_auth():
    """Create a connection to Google Drive

    On first use, a client_secrets.json file must be placed in the current directory.
    If you need guidance on where to get the client_secrets.json file, run `auth-help`.
    
    Or consult project readme. Source: https://github.com/wrecodde/pusher#readme

    If authentication has been previously set up, authentication credentials would be loaded
    from saved file.
    """

    # TODO: need to allow for expiry and refresh of tokens

    global drive 

    google_auth = GoogleAuth()
    
    if CREDS_FILE.exists():
        try:
            google_auth.LoadCredentialsFile(CREDS_FILE)
        except:
            error_message = \
            "Error occured when attempting to use saved auth credentials. \n\
            Check your internet connection. If the error persists, clear saved credentials with `clear-auth`."
            logger.error(error_message)
    else:
        logger.info('Unavailable auth credentials. Allow access with the `add-auth` command.')
        sys.exit()
    
    drive = GoogleDrive(google_auth)
Esempio n. 2
0
def upload_via_celery(self, name, file_name, cred_file):
    gauth = GoogleAuth()
    print(os.listdir(settings.MEDIA_ROOT + "/uploaded"))
    gauth.LoadCredentialsFile(settings.MEDIA_ROOT + "/" + cred_file)
    self.update_state(state='PROGRESS')

    # all_objects_dict = {"web_address":[],"original_text":[],"translated_text":[],"name":[],"hyperlink":[],"img":[],"link_to_image":[],"drive_link":[]}
    all_objects_dict = scrap_the_file(name, self)
    print(all_objects_dict)

    dataframe = pd.DataFrame(all_objects_dict)
    dataframe.drop(columns=["image_data"])
    dataframe.columns = [
        "Page", "description", "Translated Text", "Name", "hyperlink", "img",
        "link_to_image", "drive_link"
    ]

    dict = dataframe.to_dict()
    json_str = json.dumps(dict)

    if os.path.exists(settings.MEDIA_ROOT + "/" + cred_file):
        os.remove(settings.MEDIA_ROOT + "/" + cred_file)

    if os.path.exists(settings.MEDIA_ROOT + "/" + cred_file):
        os.remove(settings.MEDIA_ROOT + "/" + cred_file)

    return json_str, all_objects_dict["image_data"]
 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
Esempio n. 4
0
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
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
Esempio n. 6
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)
 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)
Esempio n. 8
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()
Esempio n. 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
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")
Esempio n. 11
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())
Esempio n. 12
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
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
Esempio n. 14
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)
Esempio n. 15
0
def upload_via_celery_home(self, name, file_name, cred_file):
    self.update_state(state='PROGRESS')
    cred_name = str(timezone.now())
    cred_name = re.sub('[\W_]+', '', "file_{}".format(cred_name)) + ".txt"
    with open(settings.MEDIA_ROOT + "/uploaded/" + cred_name + ".txt",
              "w") as file:
        file.write(cred_file)
        file.close()

    gauth = GoogleAuth()
    gauth.LoadCredentialsFile(settings.MEDIA_ROOT + "/uploaded/" + cred_name +
                              ".txt")

    # all_objects_dict = {"web_address":[],"original_text":[],"translated_text":[],"name":[],"hyperlink":[],"img":[],"link_to_image":[],"drive_link":[]}
    all_objects_dict = scrap_the_file(name, gauth, self)
    dataframe = pd.DataFrame(all_objects_dict)
    dataframe.columns = [
        "Page", "description", "Translated Text", "Name", "hyperlink", "img",
        "link_to_image", "drive_link", "image_data"
    ]
    # dict = dataframe.to_csv(settings.MEDIA_ROOT+"/"+file_name+".csv")
    dict = dataframe.to_dict()
    json_str = json.dumps(dict)

    df = dataframe.drop(columns=["image_data", "link_to_image"], axis=1)

    csv_name = re.sub(r'[\W_]+', "", str(timezone.now()))
    df.to_csv("csv_{}.csv".format(csv_name))
    drive = GoogleDrive(gauth)
    file = drive.CreateFile()
    file.SetContentFile("csv_{}.csv".format(csv_name))
    file["title"] = file_name
    file.Upload()

    if os.path.exists("csv_{}.csv".format(csv_name)):
        try:
            os.remove("csv_{}.csv".format(csv_name))
        except:
            pass
    return json_str, file.metadata["embedLink"]
Esempio n. 16
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")
import os
import requests
import shutil
from pydrive2.auth import GoogleAuth
from pydrive2.drive import GoogleDrive
import discord
from discord.ext import commands
import tension
from helpy import hell

gauth = GoogleAuth()
gauth.LoadCredentialsFile("auth.json")
if gauth.access_token_expired:
    gauth.Refresh()
else:
    gauth.Authorize()
drive = GoogleDrive(gauth)


class Cloudshit(commands.Cog, name='Cloud Transfers'):
    def __init__(self, bot):
        self.bot = bot

    @commands.command(aliases=['u', 'up'], help=hell['upload'])
    async def upload(self, ctx, title=None):

        try:
            attachment = ctx.message.attachments[0]
            fileurl = attachment.url
            if fileurl.find('/'):
                name = fileurl.rsplit('/', 1)[1]
Esempio n. 18
0
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()
Esempio n. 19
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()
Esempio n. 20
0
 def CheckCredentialsFile(self, credentials, no_file=False):
     ga = GoogleAuth(settings_file_path("test_oauth_default.yaml"))
     ga.LoadCredentialsFile(credentials)
     self.assertEqual(ga.access_token_expired, no_file)
Esempio n. 21
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 = {}
Esempio n. 22
0
 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)
Esempio n. 23
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
Esempio n. 24
0
#======Global stuffs========
scopes = ['https://www.googleapis.com/auth/drive.metadata.readonly',
          'https://www.googleapis.com/auth/drive.file',
          'https://www.googleapis.com/auth/youtube.force-ssl']

url = ['https://www.googleapis.com/upload/drive/v3/files?uploadType=resumable']
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.