can i use google chat api with service account and without any chat app from google console i use php/Laravel with google client Library. and need to get data from chat api in my project. and I need any google workspace accoun. can i use this api as service account.
protected $apiUrl;
public function __construct()
{
$this->apiUrl = 'https://chat.googleapis.com/v1';
}
protected function getAccessToken1()
{
$credentialsPath = Storage::path('credentials.json');
$credentials = json_decode(file_get_contents($credentialsPath), true);
$jwtPayload = [
'iss' => $credentials['client_email'],
'scope' => 'https://www.googleapis.com/auth/chat.spaces',
'aud' => 'https://www.googleapis.com/oauth2/v4/token',
'exp' => time() + 3600,
'iat' => time()
];
$jwt = \Firebase\JWT\JWT::encode($jwtPayload, $credentials['private_key'], 'RS256');
$client = new Client();
$response = $client->request('POST', 'https://www.googleapis.com/oauth2/v4/token', [
'form_params' => [
'grant_type' => 'urn:ietf:params:oauth:grant-type:jwt-bearer',
'assertion' => $jwt
]
]);
$accessToken = json_decode($response->getBody(), true)['access_token'];
if (!$accessToken) {
return;
}
$url = $this->apiUrl . "/spaces";
$headers = [
'Authorization' => 'Bearer ' . $accessToken,
'Content-Type' => 'application/json',
];
// $body = [
// 'text' => $messageText,
// ];
$response = Http::withHeaders($headers)->get($url);
// dd($response);
if ($response->successful()) {
return $response;
} else {
$error = $response->json();
return $error;
}
}
my code api return that
"Google Chat app not found. To create a Chat app, you must turn on the Chat API and configure the app in the Google Cloud console.