Python pydrive.auth.GoogleAuth() Examples

The following are 9 code examples of pydrive.auth.GoogleAuth(). You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may also want to check out all available functions/classes of the module pydrive.auth , or try the search function .
Example #1
Source File: gdriveutils.py    From calibre-web with GNU General Public License v3.0 6 votes vote down vote up
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: %s", e)
            except Exception as e:
                log.exception(e)
        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: %s", e)
    return drive 
Example #2
Source File: gdrive_writer.py    From exporters with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
def __init__(self, *args, **kwargs):
        super(GDriveWriter, self).__init__(*args, **kwargs)
        from pydrive.auth import GoogleAuth
        from pydrive.drive import GoogleDrive
        gauth = GoogleAuth()
        files_tmp_path = tempfile.mkdtemp()
        client_secret_file = os.path.join(files_tmp_path, 'secret.json')
        with open(client_secret_file, 'w') as f:
            f.write(json.dumps(self.read_option('client_secret')))
        gauth.LoadClientConfigFile(client_secret_file)
        credentials_file = os.path.join(files_tmp_path, 'credentials.json')
        with open(credentials_file, 'w') as f:
            f.write(json.dumps(self.read_option('credentials')))
        gauth.LoadCredentialsFile(credentials_file)
        shutil.rmtree(files_tmp_path)
        self.drive = GoogleDrive(gauth)
        self.set_metadata('files_counter', Counter())
        self.set_metadata('files_written', []) 
Example #3
Source File: gdriveutils.py    From calibre-web with GNU General Public License v3.0 5 votes vote down vote up
def __init__(self):
        self.auth = GoogleAuth(settings_file=SETTINGS_YAML) 
Example #4
Source File: authenticator.py    From GROOT with Mozilla Public License 2.0 5 votes vote down vote up
def driveAuthorization():
    """Authorizes access to the Google Drive"""
    
    gauth = GoogleAuth()
    gauth.LocalWebserverAuth() # Creates local webserver and auto handles authentication.
    drive = GoogleDrive(gauth)
    return drive 
Example #5
Source File: DenseNet_CIFAR10.py    From hacktoberfest2018 with GNU General Public License v3.0 5 votes vote down vote up
def get_from_drive(idv):
  gauth = GoogleAuth()
  gauth.credentials = GoogleCredentials.get_application_default()
  drive = GoogleDrive(gauth)
  last_weight_file = drive.CreateFile({'id': idv}) 
  last_weight_file.GetContentFile('DenseNet-40-12-CIFAR10.h5') 
Example #6
Source File: download.py    From medleydb with MIT License 5 votes vote down vote up
def authorize_google_drive():
    global GAUTH
    global DRIVE
    if GAUTH is None or DRIVE is None:
        GAUTH = GoogleAuth()
        # Creates local webserver and auto handles authentication.
        GAUTH.LoadClientConfigFile(client_config_file=GRDIVE_CONFIG_PATH)
        GAUTH.LocalWebserverAuth()
        DRIVE = GoogleDrive(GAUTH)
        return True
    else:
        return True 
Example #7
Source File: get_gdrive_credentials.py    From exporters with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def run(args):
    gauth = GoogleAuth()
    gauth.LoadClientConfigFile(args.client_secret)
    gauth.LocalWebserverAuth()
    credentials_file = os.path.join(args.dest, 'gdrive-credentials.json')
    gauth.SaveCredentialsFile(credentials_file)
    print('Credentials file saved to {}'.format(credentials_file)) 
Example #8
Source File: GoogleDriveActionBase.py    From motion-notify with GNU General Public License v3.0 5 votes vote down vote up
def authenticate(config):
        logger.debug("GoogleDriveAction starting authentication")
        svc_user_id = config.config_obj.get('GoogleDriveUploadAction', 'service_user_email')
        svc_scope = "https://www.googleapis.com/auth/drive"
        svc_key_file = config.config_obj.get('GoogleDriveUploadAction', 'key_file')
        gcredentials = ServiceAccountCredentials.from_p12_keyfile(svc_user_id, svc_key_file, scopes=svc_scope)
        gcredentials.authorize(httplib2.Http())
        gauth = GoogleAuth()
        gauth.credentials = gcredentials
        logger.debug("GoogleDriveUploadAction authentication complete")
        return gauth 
Example #9
Source File: uploader.py    From lightnovel-crawler with Apache License 2.0 4 votes vote down vote up
def upload(file_path, description=None):
    try:
        gauth = GoogleAuth()
        # gauth.LocalWebserverAuth()

        # Try to load saved client credentials
        credential_file = os.getenv('GOOGLE_DRIVE_CREDENTIAL_FILE')
        gauth.LoadCredentialsFile(credential_file)
        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()
        # end if

        # Save the current credentials to a file
        gauth.SaveCredentialsFile(credential_file)

        drive = GoogleDrive(gauth)
        folder_id = os.getenv('GOOGLE_DRIVE_FOLDER_ID')
        filename_w_ext = os.path.basename(file_path)
        filename, file_extension = os.path.splitext(filename_w_ext)

        # Upload file to folder
        f = drive.CreateFile(
            {"parents": [{"kind": "drive#fileLink", "id": folder_id}]})
        f['title'] = filename_w_ext

        # Make sure to add the path to the file to upload below.
        f.SetContentFile(file_path)
        f.Upload()

        logger.info(f['id'])
        return f['id']
    except Exception:
        logger.exception('Failed to upload %s', file_path)
    # end try
    return None
# end def