首页 > 解决方案 > 任何人都可以用 php 分享工作的环聊聊天 api 示例吗?

问题描述

我正在寻找带有服务帐户的 Google 聊天 API 代码。我尝试了以下代码,但出现了一些错误,不确定我缺少什么。

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();
    }

{“错误”:{“代码”:404,“消息”:“未找到请求的实体。”,“错误”:[{“消息”:“未找到请求的实体。”,“域”:“全局” ", "原因": "notFound" } ], "状态": "NOT_FOUND" } }

标签: hangouts-chat

解决方案


考虑

注意:最近对 google-api-php-client Github 存储库的提交使用命名空间表示法更新了类名。

您正在使用命名空间表示法 ( Google\Client()) 而未事先激活它。如果您不打算使用命名空间,则应采用不同的表示法。

解决方案

请参考此语法以Google_Client在您的 PHP 脚本中使用 PHP 类方法:

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();
}

参考

PHP 命名空间

谷歌 PHP API 服务帐号


推荐阅读