flowpack/googleapiclient

Neos Flow 的 Google API 客户端提供者

1.3.0 2022-04-22 16:05 UTC

This package is auto-updated.

Last update: 2024-08-22 21:06:05 UTC


README

一个提供 Neos Flow & CMS 包以及 Google API 客户端认证和辅助工具的包。

安装

像这样将依赖添加到您的站点包中

composer require --no-update flowpack/googleapiclient

然后在您的项目根目录中运行 composer update

配置

认证

首先从 Google 获取您的 auth.json 文件。

为了允许 API 客户端进行认证,您必须通过以下命令存储凭证

./flow googleapi:storecredentials auth.json

或者设置环境变量 GOOGLE_APPLICATION_CREDENTIALS 为您存储 auth.json 的路径。

应用程序名称

在您的 Settings.yaml 中设置应用程序名称,如下所示

Flowpack:
  GoogleApiClient:
    applicationName: 'My app' 

用法

将以下内容的 Objects.yaml 添加到您的包的 Configuration 文件夹中,并根据您的服务名称进行修改

Vendor\Package\Service\MyGoogleService:
  arguments:
    1:
      object:
        factoryObjectName: Flowpack\GoogleApiClient\Service\ClientFactory
        factoryMethodName: create

然后让您的 MyGoogleService 类看起来像这样

<?php
namespace Vendor\Package\Service;

use Google_Client;
use Neos\Flow\Annotations as Flow;

/**
 * My Google API service
 *
 * @Flow\Scope("singleton")
 */
class MyGoogleService extends \Google_Service_Webmasters
{
    /**
     * @inheritdoc
     */
    public function __construct(Google_Client $client)
    {
        parent::__construct($client);

        $client->addScope(\Google_Service_Webmasters::WEBMASTERS);
    }
}

在这个例子中,它将使用 Google 网站管理员端点的 API。

您可以通过更改继承和设置构造函数中的正确作用域来调整此内容,以适应您所需的 API。

最后,实现访问 API 的方法,并在需要的地方注入服务。