Search code examples
pythongoogle-cloud-functionscloud-document-ai

Permission denied when invoking Document AI v1beta3 from Cloud Function


I'm trying to call to DocumentAI v1beta3 from Cloud Function with the code snippet as follow:

client = documentai.DocumentProcessorServiceClient()

input_doc = documentai.types.Document(
    content=file_to_be_extract,
    mime_type='application/pdf')

request = documentai.types.ProcessRequest(
    name='DocumentFieldExtract',
    document=input_doc)

processResponse = client.process_document(request=request)
document = processResponse.document

# All text extracted from the document
print('Document Text: {}'.format(document.text))

where the file_to_be_extract is in bytes type and DocumentFieldExtract is the DocumentAI processor name.

I'm getting error Permission denied on resource project DocumentFieldExtract after running the code above. FYI, I have tried to assign Cloud DocumentAI Administrator role to the Cloud Function service account.

Is there anything else that I need to do in order to grant?

If I may, I would also like to know what are the differences between v1beta2 and v1beta3.

Thanks!


Solution

  • Apparently the name param for the processor should be in the format of projects/{project_id}/locations/{location}/processors/{processor_id} not just the processor name that we created in Document AI console.

    Source: https://cloud.google.com/blog/topics/developers-practitioners/using-document-ai-automate-procurement-workflows