0

I have an Angular app, developed in Angular 8 using Google Earth Engine for NDVI mapping.

Below is the basic code tried initially:

ee.data.authenticateViaPrivateKey(PRIVATE_KEY_JSON_FILE_PATH);

Since, it showed an error so tried to call this file with microservice API call

this.googleapi.authGoogleEarthEngineService().subscribe(
    (data) => {
        ee.data.authenticateViaPrivateKey(data);
        // HERE data HAS RECEIVED THE JSON FILE CORRECT. CHECKED WITH CONSOLE.LOG FUNCTION
        return data;
    },
    (err: HttpErrorResponse) => {
        // ERROR HANDLING
    }
);

I tried microservice approach because a connection of mine suggested to use server-side call to this privatekey JSON file.

Also, tried this code to see if something else isn't the issue:

        ee.data.authenticateViaPrivateKey(
            PRIVATE_KEY, () => {
                console.log('Authentication succeeded!');
                // Attempt to initialize Earth Engine.
                ee.initialize(
                    null, null,
                    // On a successful initialize
                    () => {
                        console.log('Successfully initialized the EE client library.');
                        app.listen(PORT);
                        console.log(`Listening on port ${PORT}`);
                        res.send("Authenticated and initialized!\n");
                    },
                    // On a failure to initialize
                    (err) => {
                        console.log(err);
                        console.log(`Initialization failed.`);
                    }
                );
            },
            // When the authentication failed.
            (err) => {
                console.log(err);
                console.log('Authentication failed');
            }
        );

In either case, the error is same i.e.

ERROR Error: Use of private key authentication in the browser is insecure. Consider using OAuth, instead.

enter image description here

With ee.data.authenticateViaOauth(this.CLIENT_ID), it logs permission denied error when any email ID other than email ID associated with Google Earth billing is used. So, that approach also doesn't work out for me. However, I've checked Google cloud console and billing. Everything seems good.

Any guidance or resource in the right direction?

Edit:

After considering Daniel's answer, if I try with OAuth then it authenticates only with the email ID associated in the billing. Is this a normal behavior or am I missing something in the process?

1 Answer 1

1

If you look at the docs, the privateKey argument in ee.data.authenticateViaPrivateKey(privateKey, success, error, extraScopes) should be the "JSON content of private key". So, not the path of the JSON file, which I assume your PRIVATE_KEY_JSON_FILE_PATH contains.

ee.data.authenticateViaPrivateKey(
  {client_email, private_key},
  () => console.log('Authentication succeeded'),
  error => console.error('Authentication failed:')
)
4
  • What's client email here? Commented Jun 9, 2020 at 14:01
  • Angular runs the code on front-end. So, does this private key authentication runs on front-end or not? Commented Jun 9, 2020 at 14:05
  • This is not code that should be run on the frontend, but that's how you use authenticateViaPrivateKey(). client_email is part of your JSON file. Commented Jun 9, 2020 at 14:12
  • Okay. Thanks Daniel. Commented Jun 9, 2020 at 15:01

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.