本文整理匯總了Python中pydrive.auth.GoogleAuth方法的典型用法代碼示例。如果您正苦於以下問題:Python auth.GoogleAuth方法的具體用法?Python auth.GoogleAuth怎麽用?Python auth.GoogleAuth使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類pydrive.auth
的用法示例。
在下文中一共展示了auth.GoogleAuth方法的9個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。
示例1: getDrive
# 需要導入模塊: from pydrive import auth [as 別名]
# 或者: from pydrive.auth import GoogleAuth [as 別名]
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
示例2: __init__
# 需要導入模塊: from pydrive import auth [as 別名]
# 或者: from pydrive.auth import GoogleAuth [as 別名]
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', [])
示例3: __init__
# 需要導入模塊: from pydrive import auth [as 別名]
# 或者: from pydrive.auth import GoogleAuth [as 別名]
def __init__(self):
self.auth = GoogleAuth(settings_file=SETTINGS_YAML)
示例4: driveAuthorization
# 需要導入模塊: from pydrive import auth [as 別名]
# 或者: from pydrive.auth import GoogleAuth [as 別名]
def driveAuthorization():
"""Authorizes access to the Google Drive"""
gauth = GoogleAuth()
gauth.LocalWebserverAuth() # Creates local webserver and auto handles authentication.
drive = GoogleDrive(gauth)
return drive
示例5: get_from_drive
# 需要導入模塊: from pydrive import auth [as 別名]
# 或者: from pydrive.auth import GoogleAuth [as 別名]
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')
示例6: authorize_google_drive
# 需要導入模塊: from pydrive import auth [as 別名]
# 或者: from pydrive.auth import GoogleAuth [as 別名]
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
示例7: run
# 需要導入模塊: from pydrive import auth [as 別名]
# 或者: from pydrive.auth import GoogleAuth [as 別名]
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))
示例8: authenticate
# 需要導入模塊: from pydrive import auth [as 別名]
# 或者: from pydrive.auth import GoogleAuth [as 別名]
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
示例9: upload
# 需要導入模塊: from pydrive import auth [as 別名]
# 或者: from pydrive.auth import GoogleAuth [as 別名]
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