Python integration with Google Drive API authentication issues

I’m working on a Python application that needs to connect to Google Drive API to list files and directories. I followed the official documentation but I keep running into authentication problems.

First I got an error about missing ‘client_secret’ field, then after fixing the JSON file naming I got another error saying “Authorized user info was not in the expected format, missing fields client_secret, client_id, refresh_token.”

I also want to make sure my app can access files in nested folders. Here’s my current code:

API_SCOPES = ['https://www.googleapis.com/auth/drive.metadata.readonly']

def run_drive_app():
    """Demonstrates Drive v3 API usage.
    Lists names and IDs of first 10 accessible files.
    """
    credentials = None
    
    if os.path.exists('auth_token.json'):
        credentials = Credentials.from_authorized_user_file('auth_token.json', API_SCOPES)
   
    if not credentials or not credentials.valid:
        if credentials and credentials.expired and credentials.refresh_token:
            credentials.refresh(Request())
        else:
            auth_flow = InstalledAppFlow.from_client_secrets_file(
                'client_secrets.json', API_SCOPES)
            credentials = auth_flow.run_local_server(port=0)
      
        with open('auth_token.json', 'w') as token_file:
            token_file.write(credentials.to_json())

    drive_service = build('drive', 'v3', credentials=credentials)

    response = drive_service.files().list(
        pageSize=10, fields="nextPageToken, files(id, name)").execute()
    file_list = response.get('files', [])

    if not file_list:
        print('No files detected.')
    else:
        print('Available files:')
        for file_item in file_list:
            print(u'{0} ({1})'.format(file_item['name'], file_item['id']))

if __name__ == '__main__':
    run_drive_app()

How can I fix these authentication errors and ensure my app works with subdirectories too?

Had this exact same issue six months ago building a backup tool. That error usually means your auth_token.json is corrupted or has the wrong credential type. Quickest fix: nuke both files and start over. Delete auth_token.json and client_secrets.json, then grab fresh OAuth 2.0 credentials from Google Cloud Console as “Desktop Application” type. Run your code again and it’ll pop open the browser auth flow to create a clean token. For nested folders - your code only sees the root directory right now. You’ll need a recursive function that checks each item’s mimeType and makes extra API calls for folders. Also, ditch metadata.readonly and use drive.readonly instead. More flexibility, same auth complexity.

check your client_secrets.json structure from google cloud console. you probably downloaded the wrong credential type - make sure it’s for desktop app, not web app. also delete auth_token.json and regenerate it, sometimes it corrupts during testing.

enable the drive api in google cloud console first - that’s the step everyone forgets. if you’re still having issues, try running the auth flow in incognito mode since browser cache can mess with oauth. your scope looks good tho.

Those authentication errors usually come from mismatched credential files or scope problems. I ran into the same thing when I started with Google Drive API. Double-check that your client_secrets.json is downloaded as ‘Desktop application’ credentials from Google Cloud Console - not web application. That’s a super common mistake that causes exactly what you’re seeing. Your current code only grabs the first 10 files from the root folder. To get into subdirectories, you need recursive folder scanning. Check each item’s mimeType for ‘application/vnd.google-apps.folder’ then call the API again using that folder ID as the parent. Also bump your scope to ‘https://www.googleapis.com/auth/drive.readonly’ if you need wider file access.

Google API auth is absolutely brutal. Been there way too many times building file tools for teams.

The core issue? Google’s auth flow breaks constantly. Even when it works, managing tokens and expired credentials in production is a total nightmare.

I stopped fighting OAuth complexity and just automate the whole Google Drive integration now. Last month I needed a similar file listing system - used Latenode to handle auth automatically. No token headaches.

You can build workflows that list files through all subdirectories without writing any auth code. Latenode manages OAuth, token refresh, and API calls for you.

For nested folders, create a workflow that automatically crawls your entire directory structure and spits out exactly what you need. No mimeType checking or recursive API calls.

Check it out: https://latenode.com