Pydrive google drive automate authentication

前端 未结 1 2075
故里飘歌
故里飘歌 2021-01-05 05:07

I have the following piece of code:

from pydrive.auth import GoogleAuth

gauth = GoogleAuth()
gauth.DEFAULT_SETTINGS = {\'save_credentials\': True,\'client_c         


        
1条回答
  •  攒了一身酷
    2021-01-05 05:52

    Actually you need to copy the client_secret.json file to my_cred.txt by the following code:

    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")
    

    Then use the following code to initialize the drive:

    def authorize_drive():
        gauth = GoogleAuth()
        gauth.DEFAULT_SETTINGS['client_config_file'] = "client_secret.json"
        gauth.LoadCredentialsFile("mycreds.txt")
        return GoogleDrive(gauth)
    
    
    class DriveReport(object):
        def __init__(self):
            self.drive = authorize_drive()    
    

    See more of this link: Automating pydrive verification process

    0 讨论(0)
提交回复
热议问题