Search code examples
pythongoogle-apigoogle-drive-apigoogle-oauthpydrive

Python: Google Drive Authentification


My task is to connect to Google Drive API (with the help of PyDrive module) and download some files. I somehow managed to get this thing working on my local computer - I registered my "app" at the Google Console, I downloaded the client_secret.json, ran the script, the authentification window popped out, I signed in with my Google account and Drive was accessible, everything OK.

Now I want to use my script on a server and I am basically clueless how to do that. I submitted a support ticket to my provider and their answer was:


You need to get these:

{
 "#authJson": "{"access_token":"XXX","token_type":"Bearer","expires_in":3600,"refresh_token":"YYY","created":1457455916}",
  "#appKey": "key",
  "#appSecret": "secret"
}

where #authJson is a result of 'whoevers-drive-you-want-to-access' authorization and and #appKey a #appSecret come from the oauth.


I don't know how exactly get these. I know how to download client_secret.json. So the question is: how to get these? And am I even on the right track? Or different approach is required.

The ideal final state would be: to have some sort of permanent access_token to my Google Drive which I can pass to the app (e.g. as a string parameter). The app then connects to my Drive and downloads desired files.


Solution

  • EDIT: See edit for pre-authorised server applications.

    What you are looking for is CommandLineAuth() from PyDrive.

    Your code should look something like this:

    from pydrive.auth import GoogleAuth
    from pydrive.drive import GoogleDrive 
    
    
    ga = GoogleAuth()
    ga.CommandLineAuth()  # This line in your code currently calls LocalWebserverAuth()
    drive = GoogleDrive(self.ga)
    # etc.
    

    When you run the script on the server, it will ask you to copy-paste a link into your local browser. Once you login with a specific account, you will be given a seemingly random string of letters and numbers. Paste that string into your console and you should be good to go.

    Since you likely won't want to do this everytime the script runs, consider adding a settings.yaml file to your project, which allows you to save log-in credentials. Details of how this is setup can be found in the docs.


    EDIT: If you want to distribute a PyDrive script to any server without further authentication, you have to:

    1. Download client_secret_xxxxx.json
    2. Add a settings.yaml file to your project, see this template. Ensure that save_credentials_file: is set, e.g. creds.json
    3. Copy the information such as client-secret from the client_secret_xxxx.json into the settings.yaml file.
    4. Go through the auth process on our development machine - you should now have an extra file in your folder containing the credentials (e.g. creds.json)
    5. Copy the entire directory containing the a) the original script(s), b) the settings.yaml file, and c) the generated creds.json file to the remote machine. Make sure relative paths don't change.
    6. Profit?

    Note: There are API call-limits attached to the Google Drive API preventing more than 100 calls within a 100 second period.