Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Google drive api with python

I'm trying to make an app where it displays the files and folders inside my google drive folder so I went to google apis and used the code I found on their page its here below

as I didn't expect it just let me log into my drive and then it returned me an error I'm providing a photo for it This is the error returned it says that

Message='client_secret' Source=D:\Study\Computer Science\G10\Paython 2nd semester\python consol app\python consol app\python_consol_app.py StackTrace: File "D:\Study\Computer Science\G10\Paython 2nd semester\python consol app\python consol app\python_consol_app.py", line 48, in main()

I managed to skip this error and renaming the json file and I solved this error only to find another error that says

Message=Authorized user info was not in the expected format, missing fields client_secret, client_id, refresh_token. Source=D:\Study\Computer Science\G10\Paython 2nd semester\python consol app\python consol app\python_consol_app.py StackTrace: File "D:\Study\Computer Science\G10\Paython 2nd semester\python consol app\python consol app\python_consol_app.py", line 48, in main()

so I'm wondering how should I solve such an issue also if my application can run into every single folder and subfolder

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

def main():
    """Shows basic usage of the Drive v3 API.
    Prints the names and ids of the first 10 files the user has access to.
    """
    creds = None
    
    if os.path.exists('token.json'):
        creds = Credentials.from_authorized_user_file('token.json', SCOPES)
   
    if not creds or not creds.valid:
        if creds and creds.expired and creds.refresh_token:
            creds.refresh(Request())
        else:
            flow = InstalledAppFlow.from_client_secrets_file(
                'credentials.json', SCOPES)
            creds = flow.run_local_server(port=0)
      
        with open('token.json', 'w') as token:
            token.write(creds.to_json())

    service = build('drive', 'v3', credentials=creds)

    
    results = service.files().list(
        pageSize=10, fields="nextPageToken, files(id, name)").execute()
    items = results.get('files', [])

    if not items:
        print('No files found.')
    else:
        print('Files:')
        for item in items:
            print(u'{0} ({1})'.format(item['name'], item['id']))

if __name__ == '__main__':
    main()

like image 261
Ahmed El-Nagar Avatar asked Jun 25 '26 04:06

Ahmed El-Nagar


1 Answers

Answer

First things first. It looks like you have some problem with your credentials. I would download the credentials file from the Quickstart again. Then, delete the generated token file in your working directory.

If you want to go deeper, you can create a GCP project, and enable the Google API as well as generate the credentials, but this approach is not necessary if you don't want it.

Once you have Quickstart running, you can focus on your task. To display the files within your Drive, there is the Files: list method, which returns all the files and folders in the current user's My Drive. To perform specific searches, you can use different parameters, but the most important is the query. You can read this guide for some usage examples and further explanation.

References:

  • Drive API: Python Quickstart
  • GCP: Creating and managing projects
  • GCP: Enabling an API
  • GCP: Setting up OAuth 2.0
  • Drive API: Files: list
  • Drive: Search for files and folders
like image 112
fullfine Avatar answered Jun 26 '26 19:06

fullfine