Accessing specific collection data with Python for Google Docs API

I’m trying to figure out how to get the contents of a particular collection using Python and the Google Docs API. Here’s what I’ve done so far:

docs_service = gdata.docs.service.DocsService()
docs_service.ClientLogin('username', 'password')

COLLECTION_FEED1 = '/feeds/documents/private/full/-/collection'
COLLECTION_FEED2 = '/feeds/default/private/full/collection%3A'

result = docs_service.Query(uri=COLLECTION_FEED1 + '?title=MyCollection&title-exact=true')
full_identifier = result.entry[0].resourceId.text
(resource_type, resource_id) = full_identifier.split(':')

result = docs_service.Query(uri=COLLECTION_FEED2 + resource_id + '/contents')

for item in result.entry:
    print(item.title.text)

The first Query works fine and gives me a valid resource ID. But when I try the second Query, I get this error:

{'status': 400, 'body': 'Invalid request URI', 'reason': 'Bad Request'}

Can anyone help me fix this? What am I doing wrong?

It appears that the issue is caused by using an outdated API. The gdata library has been deprecated, and its methods no longer support current Google Docs functionality. Today, Google recommends switching to the official Google Docs API along with the google-auth and google-auth-oauthlib libraries.

You would need to set up your OAuth 2.0 credentials, then use the googleapiclient.discovery module to create a modern Docs service object. Once authenticated, you can retrieve your data by listing files and obtaining their details. This method not only resolves the error but also provides better support and clear examples in the official documentation.

I’ve been down this road before, and I feel your frustration. The gdata library is indeed a headache these days. What worked for me was switching to the Google Drive API in conjunction with the Docs API. It’s a bit more setup initially, but it’s smoother sailing afterward.

First, you’ll need to set up a project in the Google Cloud Console and enable both APIs. Then, use the google-auth library for authentication. Here’s a snippet that might help:

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

creds = Credentials.from_authorized_user_file('token.json', ['https://www.googleapis.com/auth/drive.readonly'])
drive_service = build('drive', 'v3', credentials=creds)
docs_service = build('docs', 'v1', credentials=creds)

# Now you can query for your collection and its contents
results = drive_service.files().list(q="'root' in parents and mimeType='application/vnd.google-apps.folder' and name='MyCollection'").execute()
folder = results.get('files', [])[0]

# Get contents of the folder
contents = drive_service.files().list(q=f"'{folder['id']}' in parents").execute()

for item in contents.get('files', []):
    print(item['name'])

This approach has been much more reliable for me. Hope it helps!

hey mate, i had similar issues. try using the newer google docs api instead of gdata. it’s way easier to work with and has better docs. you’ll need to set up oauth2 creds first tho. check out the googleapiclient.discovery module, it’ll make your life easier trust me