-3

I am looking for Google chat api code with service account. I tried following code but getting some errors, not sure what I am missing.

include_once BP."/lib/google-api/vendor/autoload.php";

$client = new Google\Client();
$client->setAuthConfig(BP."/scripts/hangout/mytee-products-e6e5368c4246.json");
$client->setApplicationName("Client_Library_Examples");
$client->setScopes(['https://www.googleapis.com/auth/chat.bot']);

try{
        $service = new Google_Service_HangoutsChat( $client );
        print_r($service->spaces->listSpaces());
    }
    catch(Exception $e){
        print $e->getMessage();
    }

{ "error": { "code": 404, "message": "Requested entity was not found.", "errors": [ { "message": "Requested entity was not found.", "domain": "global", "reason": "notFound" } ], "status": "NOT_FOUND" } }

Vipin Garg
  • 466
  • 3
  • 15

1 Answers1

1

Consideration

Note: A recent commit to the google-api-php-client Github repository updated class names with namespace notation.

You are using namespace notation (Google\Client()) while not activating it beforehand. You should adopt a different notation if you don't intend to use namespaces.

Solution

Please refer to this syntax to use Google_Client PHP Class methods in your PHP script:

include_once __DIR__ . '/path/to/vendor/autoload.php';

$client = new Google_Client();

$client->setAuthConfig(__DIR__."/path/to/credentials.json");
$client->setApplicationName("Your_Application_Name");
$client->setScopes(['https://www.googleapis.com/auth/chat.bot']);

try {
        $service = new Google_Service_HangoutsChat( $client );
        print_r($service->spaces->listSpaces());
} catch(Exception $e) {
        print $e->getMessage();
}

References

PHP Namespaces

Google PHP API Service Account

Alessandro
  • 2,848
  • 1
  • 8
  • 16
  • Sorry but it is not making any difference. Still not working and showing same error. – Vipin Garg Nov 05 '20 at 16:28
  • It works on my side, what is the line of your code that produces the 404 error? – Alessandro Nov 06 '20 at 08:55
  • showing exception { "error": { "code": 404, "message": "Requested entity was not found.", "errors": [ { "message": "Requested entity was not found.", "domain": "global", "reason": "notFound" } ], "status": "NOT_FOUND" } } May be i am missing settings ? – Vipin Garg Nov 06 '20 at 15:36
  • Yes, what's the line that returns this exception? – Alessandro Nov 06 '20 at 16:07
  • i got the solution by google team, i was missing bot configuration at https://console.developers.google.com/apis/api/chat.googleapis.com/hangouts-chat?project={your-project}&pli=1&authuser=1 – Vipin Garg Nov 09 '20 at 11:44