Python script fails to generate new document using Google Docs API - what am I missing?

I’m working on a Python project that needs to generate documents through the Google Docs API. I’ve been following the official documentation but running into issues.

Here’s my current attempt:

import os
import json
from google.oauth2.credentials import Credentials
from google_auth_oauthlib.flow import InstalledAppFlow
from googleapiclient.discovery import build
from google.auth.transport.requests import Request

SCOPES = ['https://www.googleapis.com/auth/documents']

def create_document():
    credentials = None
    
    if os.path.exists('auth_token.json'):
        with open('auth_token.json', 'r') as file:
            credentials = Credentials.from_authorized_user_file('auth_token.json', SCOPES)
    
    if not credentials or not credentials.valid:
        if credentials and credentials.expired and credentials.refresh_token:
            credentials.refresh(Request())
        else:
            auth_flow = InstalledAppFlow.from_client_secrets_file(
                'client_secret.json', SCOPES)
            credentials = auth_flow.run_local_server(port=8080)
        
        with open('auth_token.json', 'w') as file:
            file.write(credentials.to_json())
    
    docs_service = build('docs', 'v1', credentials=credentials)
    
    document_data = {
        'title': 'New Report'
    }
    
    result = docs_service.documents().create(body=document_data).execute()
    print(f'Document created: {result.get("title")}')

if __name__ == '__main__':
    create_document()

The script runs without errors but prints “Document created: None” instead of showing the actual title. I’ve tried different permission scopes and authentication methods but still can’t get it working properly. Has anyone encountered this before? What could be causing the document creation to fail silently?

Your logic looks right, but getting None means the API response structure isn’t what you’re expecting. I’ve hit this before - the create() method returns the full document object, but nested properties can be weird to access. Print the entire result first with print(result) to see what’s actually coming back. Also double-check that your client_secret.json is set up right in Google Cloud Console with Docs API enabled. I’ve seen the API work but permissions get messed up, giving you partial responses. The document might actually be getting created fine even though the title shows as None.

the issue is prob how ur gettin the result. try using result.get('documentId') to see if it created the doc, then fetch the title with docs_service.documents().get(documentId=doc_id).execute(). create method dosent always give title back directly.

Had the same issue with the Docs API last month. The create method returns the document object right away, but the title field often comes back as null even though the document gets created fine. It’s just a timing thing - there’s a slight delay in API processing. Here’s what fixed it for me: add a small delay after creation, then do a separate get request for the full details. Try time.sleep(1) after your create call, then use docs_service.documents().get(documentId=result['documentId']).execute() to grab the complete info. The documentId should be there in your initial result even if the title isn’t. Also check your Google Cloud Console to make sure the documents are actually showing up in Drive.