Daní
Daní

Reputation: 375

Ho can I Invoke GCP DocumentAI API from App Engine .NET service?

I have a .NET Core GCP App Engine service that need to use GCP Document AI API. Unfortunately there's no .netcore client to access Document AI API yet so I guess I'll need to access using an API Rest call.

How I can get the access token to set the authorization header in the request.

Upvotes: -1

Views: 414

Answers (3)

Holt Skinner
Holt Skinner

Reputation: 2234

The Documentation for Document AI has been updated to include information about the C#/.NET Client Library, including a Quickstart Code Sample.

https://cloud.google.com/document-ai/docs/libraries#client-libraries-install-csharp

using Google.Cloud.DocumentAI.V1;
using Google.Protobuf;
using System;
using System.IO;

public class QuickstartSample
{
    public Document Quickstart(
        string projectId = "your-project-id",
        string locationId = "your-processor-location",
        string processorId = "your-processor-id",
        string localPath = "my-local-path/my-file-name",
        string mimeType = "application/pdf"
    )
    {
        // Create client
        var client = new DocumentProcessorServiceClientBuilder
        {
            Endpoint = $"{locationId}-documentai.googleapis.com"
        }.Build();

        // Read in local file
        using var fileStream = File.OpenRead(localPath);
        var rawDocument = new RawDocument
        {
            Content = ByteString.FromStream(fileStream),
            MimeType = mimeType
        };

        // Initialize request argument(s)
        var request = new ProcessRequest
        {
            Name = ProcessorName.FromProjectLocationProcessor(projectId, locationId, processorId).ToString(),
            RawDocument = rawDocument
        };

        // Make the request
        var response = client.ProcessDocument(request);

        var document = response.Document;
        Console.WriteLine(document.Text);
        return document;
    }
}

Here is the full API Spec for the C#/.NET Client Library.

https://cloud.google.com/dotnet/docs/reference/Google.Cloud.DocumentAI.V1/latest

Upvotes: 0

Javi
Javi

Reputation: 378

There is a beta version, you can get the following Nuget Package: Google.Cloud.DocumentAI.V1Beta2 (as per today).

Add the reference and the using:

using Google.Cloud.DocumentAI.V1Beta2;

Then you can do similar as with the other Cloud components, Build a Service Client and use it, as simple as:

var client = new DocumentUnderstandingServiceClientBuilder {
    CredentialsPath = "google-credentials.json"
}.Build();

You can then use the client for whatever your needs are.

Regarding the google-credentials.json file, you can find all the information here. In brief: get your credentials into a file and create an environmental variable named "GOOGLE_APPLICATION_CREDENTIALS" to point to that file.

Upvotes: 1

Daní
Daní

Reputation: 375

I found 2 different ways:

First one: Just write a simple NodeJS App Engine Service (or java or python as Document AI currently offers client libraries for these 3 languages) that acts as a proxy between the .netcore service and the Document AI api.

Second One: Create an Api Key in the CGP project and grab it as an environment variable in the .netcore service. Then, just send and http post to the url adding the api key as a query param like:

https://eu-documentai.googleapis.com/v1beta2/projects/{YOUR_PROJECT_ID}/locations/eu/documents:process?key={API_KEY}

And that's it.

About the second one, I'd prefer not having to use an ApiKey. I'd like to know how client libraries such as nodejs's manage to send the request to DocumentAI api without the ApiKey. In my first attempt, I expected that using the HttpClient from GCP Service Account Credential would do the trick, but I got a 403 error pointing that an api key was missing. What I did was:

var cred = GoogleCredential.GetApplicationDefault()
var credential = cred.UnderlyingCredential as ServiceAccountCredential;
var uri = new Uri($"https://eu-documentai.googleapis.com/v1beta2/projects/{YOUR_PROJECT_ID}/locations/eu/documents:process;
var responseMessage = await credential.HttpClient.PostAsJsonAsync(uri, config);
var response = await responseMessage.Content.ReadAsStringAsync();

As already said, this produces a 403 error complaining about missing an ApiKey. When using the ApiKey you can use a regular HttpClient, using the HttpClient created for the Google Credential is not required

Upvotes: 2

Related Questions