How to generate multiple Google Documents using background queue processing

I’m working on a project where I need to generate several Google Documents through background processing. I decided to implement the taskqueue feature from Google App Engine for this purpose, but something isn’t working correctly.

Whenever I try to run my tasks, I keep seeing this error pattern in my logs:

INFO     2016-05-17 15:38:46,393 module.py:787] default: "POST /create_document HTTP/1.1" 302 -
WARNING  2016-05-17 15:38:46,393 taskqueue_stub.py:1981] Task document_task failed to execute. This task will retry in 0.800 seconds

Here’s my implementation. I have a handler called CreateDocument that should process individual document creation requests from the queue:

# Handler for creating individual documents (processes queue tasks)
class CreateDocument(BaseHandler):
    @decorator.oauth_required
    def post(self):
        try:
            http_auth = decorator.http()
            drive_service = discovery.build("drive", "v2", http=http_auth)

            # Generate the document
            document_title = self.request.get('document_title')
            file_body = {
                'mimeType': DOCS_MIMETYPE,
                'title': document_title,
            }
            drive_service.files().insert(body=file_body).execute()
        except AccessTokenRefreshError:
            self.redirect("/")


# Handler that queues multiple document creation tasks
class BatchDocumentGenerator(BaseHandler):
    def get(self):
        try:
            for counter in range(5):
                filename = "Document_" + str(counter)
                taskqueue.add(
                    url='/create_document',
                    params={
                        'document_title': filename,
                    })
            self.redirect('/documents')
        except AccessTokenRefreshError:
            self.redirect('/')

I can see all the tasks appearing in the App Engine Console’s task queue section, but they just sit there and won’t execute properly. What could be causing this issue?

That 302 redirect means your CreateDocument handler is hitting the OAuth decorator and trying to redirect for authorization. This breaks background tasks since there’s no user session. You’ve got two options: use service account credentials for server-to-server auth, or store the user’s refresh token in datastore so you can manually refresh it in your task handler. The OAuth decorator only works for user-facing requests where someone can actually complete the auth flow.

Your task queue handler has @decorator.oauth_required, but background tasks don’t have user sessions. The decorator can’t find OAuth credentials, so it returns a 302 redirect and your tasks keep failing.

I’ve hit this same issue. Best fix: let your main handler (BatchDocumentGenerator) handle OAuth, then pass those credentials to your queued tasks. Serialize the access token and include it in the task parameters. Your CreateDocument handler can use those credentials directly instead of relying on the decorator.

If you’re generating docs for your app (not individual users), just switch to service account auth. No interactive OAuth needed.

you’re on the right track. @decorator.oauth_required needs user context, but background tasks don’t have that. use a service account for auth instead, or save the access tokens so your tasks can actually reach google drive.