ingress-it-solutions/laravel-google-my-business

一个为Laravel实现的Google My Business API的包

2.2.1 2023-02-01 10:18 UTC

This package is not auto-updated.

Last update: 2024-09-26 17:34:35 UTC


README

注意:要使用Google My Business API,您必须申请访问权限: https://docs.google.com/forms/d/1XTQc-QEjsE7YrgstyJxbFDnwmhUhBFFvpNJBw3VzuuE/viewform

这是由Google在 https://developers.google.com/my-business/samples/ 上提供的Google My Business PHP库(v4.4)的Laravel兼容实现。

有关Google My Business API功能的详细信息,请参阅 https://developers.google.com/my-business/reference/rest/ - 此文档底部提供了更多有用的链接。

调试

我将此置于所有其他内容之上,因为它非常重要。Google My Business客户端库不支持详细的错误响应: https://developers.google.com/my-business/content/support#detailed_error_responses

因此,当出现问题时,您会收到一个极其无用的400错误消息,例如“请求包含无效的参数”。

要获取更详细的错误消息,我们需要在请求中添加HTTP头: X-GOOG-API-FORMAT-VERSION: 2,返回的错误消息将更加有用。

没有很优雅的方式来做到这一点,但您只需要打开

/vendor/google/apiclient/src/Google/Http/REST.php

并在 doExecute 函数中修改如下

$httpHandler = HttpHandlerFactory::build($client);

// Add the header to the request
$request = $request->withHeader('X-GOOG-API-FORMAT-VERSION', '2');

调试完成后,请删除此添加的行。

安装

运行 composer require ingress-it-solutions/laravel-google-my-business

或者通过composer安装 - 编辑您的composer.json以要求此包。

"require": { "ingress-it-solutions/laravel-google-my-business": "2.*" }

然后在您的终端中运行 composer update 以拉取它。

Laravel

重要:强烈建议您使用 https://github.com/pulkitjalan/google-apiclient 来处理Google客户端设置。此文档下面的代码示例假设您正在使用此库。

要在Laravel项目中使用此Google My Business包,请将以下内容添加到您的 config/app.php 中的 providers 数组

IngressITSolutions\LaravelGoogleMyBusiness\GoogleMyBusinessServiceProvider::class,

接下来,将以下内容添加到您的 config/app.php 中的 aliases 数组

'GoogleMyBusiness' => IngressITSolutions\LaravelGoogleMyBusiness\GoogleMyBusiness::class

Google My Business发现文档

Google My Business API发现文档是一个JSON文档,描述了API的特定版本的表面。您与Google API发现服务一起使用发现文档。

mybusiness_google_rest_v4p4.json 已包含在此项目中供您参考(从: https://developers.google.com/my-business/samples/ 下载)

提示

代码示例

(工作正在进行中)

use Google; // See: https://github.com/pulkitjalan/google-apiclient
use GoogleMyBusiness;

class MyExampleClass
{
  function authRedirect()
  {
    // Define the GMB scope
    $scopes = ['https://www.googleapis.com/auth/plus.business.manage'];

    // Define any configs that overrride the /config/google.php defaults from pulkitjalan/google-apiclient
    $googleConfig = array_merge(config('google'), [
      'scopes' => $scopes,
      'redirect_uri' =>
        config('app.callback_url') . '/callback/google/mybusiness',
    ]);

    // Generate an auth request URL
    $googleClient = new Google($googleConfig);
    $loginUrl = $googleClient->createAuthUrl();

    // Send user to Google for Authorisation
    return redirect()->away($loginUrl);
  }

  function getAccountName(Google $googleClient)
  {
    $gmb = new GoogleMyBusiness($googleClient);
    return $gmb->getAccountName();
  }
}

致谢

感谢Scotty Bowler最初创建这个包。我们现在扩展它以用于Laravel 9。

有用链接

主要是我开发这个包时的参考,但你也可能觉得它们很有用!