bronhy/google-my-business-php-client

v4.9 2021-03-12 18:43 UTC

This package is auto-updated.

Last update: 2024-09-06 17:34:16 UTC


README

Google My Business API 客户端库封装了 Google My Business API 的功能,并提供所有 Google API 共同的功能,例如 HTTP 传输、错误处理、认证、JSON 解析和协议缓冲区支持。

本包包含 MyBusiness 服务定义,它依赖于 Google API PHP 客户端。

欢迎您贡献力量并申请维护权限。

重要公告

Google 正在更新 My Business API 并在此库中公开新的服务定义。 https://github.com/googleapis/google-api-php-client-services。更新完成后,此存储库将被标记为过时。

要求

Google API PHP 客户端 URL: https://github.com/google/google-api-php-client/releases

如何安装

最简单的方式是使用 composer。

composer require bronhy/google-my-business-php-client

语义化版本控制

请注意,Google 在此库中不遵循语义化版本控制 RFC https://semver.org/。由于此包反映了 Google 版本,请确保您锁定到您的版本。

ie. composer require bronhy/google-my-business-php-client:4.8

使用 Symfony 框架配置

# config/services.yaml

parameters:
    app.google_credentials: '%env(json:base64:GOOGLE_CREDENTIALS)%' # exported json base64 encoded
    app.client_scope: 'https://www.googleapis.com/auth/plus.business.manage'
    app.redirect_url: '%env(GMB_REDIRECT_URI)%'

services:
    # default configuration for services in *this* file
    _defaults:
        autowire: true      # Automatically injects dependencies in your services.
        autoconfigure: true # Automatically registers your services as commands, event subscribers, etc.
    
    Google_Client:
        class: Google_Client
        calls:
            - method: setAuthConfig
              arguments:
                  - '%app.google_credentials%'
            - method: addScope
              arguments:
                  - '%app.client_scope%'
            - method: setAccessType
              arguments:
                  - 'offline'
            - method: setRedirectUri # handy for local dev ie. https://:8443/index.php 
              arguments:
                  - '%app.redirect_url%'
            - method: setLogger
              arguments:
                      - '@monolog.logger'
        tags:
            - { name: monolog.logger, channel: google-api-php-client }

    Google_Service_MyBusiness:
        class: Google_Service_MyBusiness
        arguments: ['@Google_Client']
# HelloController.php
<?php

/**
 * @Route("/hello/world") name="hello_world" methods=("GET")
 *
 * @param Google_Service_MyBusiness $myBusiness

 *
 * @return JsonResponse
 */
public function helloWorld(Google_Service_MyBusiness $myBusiness)
{
    // some logic
    return JsonResponse(['HelloWorld']);
}

详细的错误响应

要启用更详细的错误消息,例如缺失的必填字段,请在您的请求中添加以下附加标题

X-GOOG-API-FORMAT-VERSION: 2

有关错误消息响应的更多信息,请参阅 Shared.Types 部分的 ErrorCode、ErrorDetail、InternalError 和 ValidationError 页面。

$client = new \Google_Client(['api_format_v2' => true]);

或使用 symfony 中的 service_dev.yaml

# config/services_dev.yaml
parameters:
    ...
    app.client_debug:
        api_format_v2: true
    ...
    Google_Client:
        class: Google_Client
        arguments: ['%app.client_debug%'] 
        calls:
    ...

认证脚本示例

服务账户

获取并包含 Google_Client,如上所述。

# src/index.php
require dirname(__DIR__) . '/vendor/autoload.php';

$client = new \Google_Client();
$client->useApplicationDefaultCredentials();
// set the service account json file location manually
$client->setAuthConfig(__DIR__ . '/Data/service_account.json');
$client->setApplicationName("_Your_app_name_here_");
// set the scope to access GMBs
$client->setScopes([
    "https://www.googleapis.com/auth/business.manage"
]);
// if you have not enabled/ have access to GMB API you need to request it directly with Google and enable it through the project's API explorer; search for "Google My Business API"