How to retrieve all root-level items in Google Docs, including shared content?

I’m working with the Google Docs API and I’m trying to figure out how to get a complete list of all items at the root level of my Google Drive. This should include both my own folders and documents, as well as any content that others have shared with me.

I found a way to list items at the root using an API endpoint, but it doesn’t show the shared stuff. Here’s what I’ve tried:

import requests

url = 'https://docs.google.com/feeds/default/private/full/folder:root/contents/'
response = requests.get(url, headers={'Authorization': 'Bearer YOUR_ACCESS_TOKEN'})

# This only shows my own content, not shared items
print(response.text)

Does anyone know how to modify this request or use a different endpoint to include shared content in the results? I’m aiming for a complete view of everything at my root level, regardless of who owns it. Thanks for any help!

I’ve tackled this issue before, and I can share what worked for me. Instead of using the Google Docs API, I found the Google Drive API to be more suitable for this task. Here’s a snippet that should get you what you’re looking for:

from googleapiclient.discovery import build

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

results = service.files().list(
    q="'root' in parents",
    fields="nextPageToken, files(id, name, mimeType, owners)",
    pageSize=1000
).execute()

items = results.get('files', [])

This query fetches all items (including shared ones) that have ‘root’ as their parent. The ‘fields’ parameter specifies what information to return for each item. You might need to handle pagination if you have more than 1000 items.

Remember to set up your credentials correctly and enable the Drive API in your Google Cloud Console. This approach has consistently given me a complete view of my Drive root, including shared content. Hope this helps!

Having worked extensively with Google’s APIs, I can confirm that the Drive API is indeed the way to go for this task. However, there’s a slight modification to Nate_91Surf’s approach that I’ve found more reliable:

Instead of using ‘root’ in parents, you can use the special ‘root’ folder ID:

results = service.files().list(
    q=\"'root' = parents or sharedWithMe = true\",
    fields=\"files(id, name, mimeType, owners, shared)\",
    pageSize=1000
).execute()

This query combines items in your root folder AND items shared with you. The ‘shared’ field in the response helps distinguish between the two. Also, don’t forget to handle pagination for large result sets. This method has consistently provided a comprehensive view of both owned and shared content at the root level in my projects.

hey SwiftCoder15, i’ve had some luck with this using the Drive API. try this:

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

creds = Credentials.from_authorized_user_file('token.json')
service = build('drive', 'v3', credentials=creds)

results = service.files().list(q="'root' in parents or sharedWithMe").execute()

this should get both ur own and shared stuff at the root. lemme know if it works!