...
// Create a client.
$chatServiceClient = new ChatServiceClient([
'credentials' => "$keyPath/songanhbot-sa-02.json",
]
);
// Prepare the request message.
$request = (new CreateMessageRequest())
->setParent($space)
->setMessage((new Message())->setText($messageText));
// Call the API and handle any network failures.
try {
$chatServiceClient->createMessage($request);
$request = (new UploadAttachmentRequest())
->setParent($space)
->setFilename($filePath);
try {
$response = $chatServiceClient->uploadAttachment($request);
} catch (ApiException $ex) {
printf('Call failed with message: %s' . PHP_EOL, $ex->getMessage());
}
} catch (ApiException $ex) {
printf('Call failed with message: %s' . PHP_EOL, $ex->getMessage());
}
I have coded this to send message and upload attachment to a Space within a Workspace.
The code can send the message successfully but failed for uploading the attachment, here is the error print from the code:
Call failed with message: {
"message": "This method doesn't support service account authentication. Authenticate with a user account.",
"code": 7,
"status": "PERMISSION_DENIED",
"details": [
{
"@type": "type.googleapis.com\/google.rpc.Help",
"links": [
{
"description": "See article Authenticate as a user.",
"url": "https:\/\/developers.google.com\/chat\/api\/guides\/auth\/users"
}
]
}
]
}
The json key file is for a service account which has already been added to Domain wide delegation via Workspace Admin(Api Controls).
The scopes enables:
I use PHP yii2 framework version: 2.0.51 and google/apps-chat version: 0.10
Please help!