Shing Ho Tan
Shing Ho Tan

Reputation: 951

Using Google Cloud AutoML Vision Model in React-Native

I am going to use a trained Google Cloud AutoML Vision Model for detecting an object and getting coordinates in React-Native. I already trained the model but I should use the token for calling the trained model. I can generate the token by using the gcloud auth application-default print-access-token But I think the token will expire and users can't call this API. What is the best solution for this problem?

fetch(
      'https://automl.googleapis.com/v1/projects/(projectid)/locations/(locationid)/models/(modelID):predict',
      {
        method: 'POST',
        headers: {
          Authorization:
            'Bearer (bearer token)',
          'Content-Type': 'application/json',
        },
        contentType: 'application/json',
        body: JSON.stringify({
          payload: {
            image: {
              imageBytes: base64Image.base64,
            },
          },
        }),
      },
    )

This is my code of react-native. Thank you for your answer.

Upvotes: 0

Views: 472

Answers (1)

Dale Markowitz
Dale Markowitz

Reputation: 191

Generally, you'd want to authenticate with a service account. This lets you download a key.json file that your program will be able to access if you export GOOGLE_APPLICATION_CREDENTIALS=path_to_your_key_file.json. Alternatively, if you don't want to keep a key file on the client, you can create a REST endpoint (for example, using a Google Cloud Function or Firebase Function) that talks to the Vision API, which can handle, for example, rate limiting. Then your React Native app would hit your wrapper REST endpoint instead of the Vision API directly.

Upvotes: 1

Related Questions