Document AI - Request had invalid authentication credentials - UNAUTHENTICATED - Staging server only

Viewed 29

I've been trying to figure out why for some reason on our staging server the following code below does not work. On localhost (a Macbook machine) it works fine, but on the server, it throws the exception. Changing service accounts, and updating packages did not help either.

For the sake of code example, mime_type and content of the file and google-service-key.json are correct. Also, the service account has the right policy permissions to perform actions against Document Ai

TLDR; on localhost it works, on the staging server it does not work

Environment details

  • OS: Ubuntu 20.04.3 LTS
  • PHP version: PHP 8.0.13
  • Package name and version:
  • "google/cloud-core": "^1.47.2" and "google/cloud-document-ai": "^0.3.0",

Error message

{
    "message": "Request had invalid authentication credentials. Expected OAuth 2 access token, login cookie or other valid authentication credential. See https:\/\/developers.google.com\/identity\/sign-in\/web\/devconsole-project.",
    "code": 16,
    "status": "UNAUTHENTICATED",
    "details": []
} {"userId":1,"exception":"[object] (Google\\ApiCore\\ApiException(code: 16): {
    \"message\": \"Request had invalid authentication credentials. Expected OAuth 2 access token, login cookie or other valid authentication credential. See https:\\/\\/developers.google.com\\/identity\\/sign-in\\/web\\/devconsole-project.\",
    \"code\": 16,
    \"status\": \"UNAUTHENTICATED\",
    \"details\": []
}

Code example

    public function __construct()
{
    // prepare Google Cloud SDK
    $this->serviceKeyPath = base_path('storage/google-service-key.json');

    if (! file_exists($this->serviceKeyPath)) {
        throw new \RuntimeException('Google Cloud Service Key not found');
    }

    $this->projectId = config('document.ocr_project_id');
    $this->location = 'eu';
    $this->processor = config('document.ocr_processor');

    // very important to set the environment variable
    putenv('GOOGLE_APPLICATION_CREDENTIALS=' . $this->serviceKeyPath);

}

public function parse(): void
{
    $rawDocument = [];
    $rawDocument['mime_type'] = '';
    $rawDocument['content'] = '';


    $inlineDocument = new RawDocument($rawDocument);

    $documentProcessorServiceClient = new DocumentProcessorServiceClient([
        'keyFile'     => json_decode(file_get_contents($this->serviceKeyPath), true, 512, JSON_THROW_ON_ERROR),
        'apiEndpoint' => 'eu-documentai.googleapis.com',
    ]);

    /** @noinspection StaticInvocationViaThisInspection */
    $formattedName = $documentProcessorServiceClient->processorName($this->projectId, $this->location, $this->processor);
    // here throws error "Request had invalid authentication credentials. Expected OAuth 2 access token, login cookie or other valid authentication credential. See https:\/\/developers.google.com\/identity\/sign-in\/web\/devconsole-project."
    $entities = $documentProcessorServiceClient->processDocument($formattedName, [
        'rawDocument' => $inlineDocument,
    ])->getDocument()?->getEntities();

}

Any help is welcome to figure out why it does not work.

Thanks!

0 Answers
Related