Python script for managing Google Docs: Creating folders and documents

I’m new to Python and trying to work with the Google Docs API. My goal is to make a script that can:

  1. Create a new folder if it doesn’t exist already
  2. Make a new document in that folder
  3. Get a list of all documents in the folder with their links

I’m using the Google Docs API 3.0 and gdata-2.0.16 for Python. Here’s what I’ve got so far:

import gdata.docs.data
import gdata.docs.client

class Config:
    APP_NAME = 'MyDocsManager-v1'

client = gdata.docs.client.DocsClient()
client.ClientLogin('my_email', 'my_password', source=Config.APP_NAME)

new_folder = gdata.docs.data.Resource(type='folder', title='MyNewFolder')
created_folder = client.CreateResource(new_folder)

new_doc = gdata.docs.data.Resource(type='document', title='TestDoc')
created_doc = client.CreateResource(new_doc, collection=created_folder)

I’m stuck on how to:

  1. Check if a folder already exists
  2. Get only the contents of a specific folder
  3. Get the full links to the documents

Any help would be really appreciated. Thanks!

hey there, i’ve been working with google docs api recently too. def agree with others about switching to drive api, it’s way easier. for checking if a folder exists, u can use files().list() with a query. creating docs in specific folders is simple with files().create(). and yeah, oauth2 is the way to go for auth. good luck with ur project!

I’ve been working with the Google Docs API for a while now, and I can share some insights based on my experience. Instead of using the older gdata library, I’d recommend switching to the newer Google Drive API, which is more powerful and better maintained.

For checking if a folder exists, you can use the ‘files().list()’ method with a query parameter to search for folders with the specific name. To get the contents of a folder, use the same method but include the folder’s ID as the ‘q’ parameter.

Getting full links to documents is straightforward with the Drive API. Each file object returned by the API includes a ‘webViewLink’ property, which gives you the URL to view the document in a browser.

One tip: make sure to use service account authentication instead of ClientLogin, as it’s more secure and flexible. You’ll need to set up credentials in the Google Cloud Console, but it’s worth the effort for better security and easier management of permissions.

I’ve implemented similar functionality using the Google Drive API v3, which offers more robust features compared to the older gdata library. Here’s a quick rundown of how you can accomplish your goals:

To check if a folder exists, use the files().list() method with a query parameter. For creating documents in a specific folder, you can use the files().create() method and specify the parent folder ID. To list documents in a folder, again use files().list() with the folder ID as a parameter.

One crucial aspect to consider is authentication. I recommend using OAuth 2.0 for server-to-server applications, which is more secure than ClientLogin. You’ll need to set up a service account and download the JSON key file.

Remember to handle API errors and implement proper exception handling in your code. Also, be mindful of API quotas and implement exponential backoff for retries if necessary.