hprotravel/symfony-gcp-rest-bundle

Google Cloud Platform REST API端点的客户端包

v0.4.0 2022-12-06 12:16 UTC

This package is auto-updated.

Last update: 2024-09-05 17:23:23 UTC


README

基于Guzzle构建的用于Google Cloud Platform REST API的Symfony Bundle

使用服务帐户凭据访问Google Cloud Platform Rest API(Google推荐的方式)

有关认证的更多信息:https://cloud.google.com/speech/docs/common/auth

安装

确保全局已安装Composer,如Composer文档中的安装章节所述。

使用Symfony Flex的应用程序

打开命令行控制台,进入您的项目目录并执行

$ composer require hprotravel/symfony-gcp-rest-bundle

不使用Symfony Flex的应用程序

步骤1:下载Bundle

打开命令行控制台,进入您的项目目录,并执行以下命令以下载此Bundle的最新稳定版本

$ composer require hprotravel/symfony-gcp-rest-bundle

步骤2:启用Bundle

然后,通过将其添加到项目config/bundles.php文件中注册的Bundle列表中,来启用该Bundle

// config/bundles.php

return [
    // ...
    GcpRestGuzzleAdapterBundle\GcpRestGuzzleAdapterBundle::class => ['all' => true],
];

步骤3:配置

gcp_rest_guzzle_adapter:
    clients:
        pubsub:
            email: 'test@test.com'
            private_key: "-----BEGIN PRIVATE KEY-----SDADAavaf...-----END PRIVATE KEY-----"
            scope: 'https://www.googleapis.com/auth/pubsub'
            project_base_url: 'https://pubsub.googleapis.com/v1/projects/test-project123/'

注意:私钥使用双引号。如果使用单引号将出现openssl_sign错误

通过容器访问服务
$pubSubClient = $container->get('gcp_rest_guzzle_adapter.client.pubsub_client');

$result = $pubSubClient->get(
    sprintf('topics/%s/subscriptions', 'test-topic')
);

var_dump((string)$result->getBody()->getContents());
结果
string(113) "{
  "subscriptions": [
    "projects/test-project123/subscriptions/test_topicSubscriber"
  ]
}
"