I've been struggling with this for hours now, if not days and can't seem to fix it.

My Requests to Cloud Functions are being denied with error code: 401: UNAUTHENTICATED.

My Code is as follow:

```
    putenv('GOOGLE_APPLICATION_CREDENTIALS=' . FIREBASE_SERIVCE_PATH);

    $client = new Google_Client();
    $client->useApplicationDefaultCredentials();
    $client->addScope(Google_Service_CloudFunctions::CLOUD_PLATFORM);
    $httpClient = $client->authorize();

    $promise = $httpClient->requestAsync("POST", "<MyCloudFunctionExecutionUri>", ['json' => ['data' => []]]);
    $promise->then(
        function (ResponseInterface $res) {
            echo "<pre>";
            print_r($res->getStatusCode());
            echo "</pre>";

        },
        function (RequestException $e) {
            echo $e->getMessage() . "\n";
            echo $e->getRequest()->getMethod();
        }
    );
    $promise->wait();

```

I'm currently executing this from localhost as I'm still in development phase.

My FIREBASE\_SERIVCE\_PATH constant links to my service\_account js

My Cloud Function index.js:

```
const functions = require('firebase-functions');
const admin = require('firebase-admin');
admin.initializeApp();

// CORS Express middleware to enable CORS Requests.
const cors = require('cors')({
  origin: true,
});

exports.testFunction = functions.https.onCall((data, context) => {
    return new Promise((resolve, reject) => {
        resolve("Ok:)");
    });
  });
  // [END all]

```

My Cloud Function Logs:
`Function execution took 459 ms, finished with status code: 401`

What am I doing wrong so I get Unauthenticated?

PS: My testFunction works perfectly when invoked from my Flutter mobile app who uses: <https://pub.dartlang.org/packages/cloud_functions>

Update:

I have followed this guide: <https://developers.google.com/api-client-library/php/auth/service-accounts> but in the "Delegating domain-wide authority to the service account" section, it only states If my application runs in a Google Apps domain, however I wont using Google Apps domain, and plus I'm on localhost.