Displaying recent Google Drive documents in App Engine project

I’m working on an App Engine project and I need some help. My app is set up to use the Google Drive API and requires users to log in. Now I want to show a list of the 25 most recent documents for each user. I’m not sure how to do this.

Does anyone have experience with this? I’ve been searching for tutorials or examples but haven’t found anything helpful yet. Here’s what I’ve got so far:

from google.oauth2.credentials import Credentials
from googleapiclient.discovery import build

def get_recent_docs(user_credentials):
    service = build('drive', 'v3', credentials=user_credentials)
    # Not sure what to do next
    return []

# In my main handler
user_docs = get_recent_docs(user.credentials)

Any tips on how to query for the most recent docs and display them would be great. Thanks!

I’ve actually tackled this exact problem in one of my projects. Here’s what worked for me:

After setting up the service, you’ll want to use the files().list() method. The key is in the parameters you pass to it. Set orderBy to ‘modifiedTime desc’ to get the most recent first, and pageSize to 25 to limit the results.

Something like this should do the trick:

results = service.files().list(orderBy=‘modifiedTime desc’, pageSize=25, fields=‘files(id, name, webViewLink)’).execute()

Then you can loop through results[‘files’] to get the info you need. Don’t forget to handle potential API errors and maybe implement some caching to avoid hitting rate limits.

Also, consider adding filters if you only want specific file types. It can help narrow down the results to exactly what you need.

In my recent project, I implemented a similar approach. After initializing the Drive service, you can use the files().list() method with parameters such as orderBy set to ‘modifiedTime desc’, pageSize set to 25, and optionally specify the fields to retrieve only the necessary data.

After retrieving the response, iterate through the items to extract file names and links. It’s crucial to incorporate error handling for API failures and consider caching to reduce repetitive calls.

hey JackHero77, i’ve worked with the Drive API before. You’re on the right track! To get recent docs, use the files().list() method with orderBy=‘modifiedTime desc’ and pageSize=25. Then iterate through the results to display. lemme know if u need more help!