teuunn/laravel-google-my-business

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

dev-master 2022-12-01 14:20 UTC

This package is not auto-updated.

Last update: 2024-09-20 21:42:34 UTC


README

注意:要使用Google My Business API,您必须申请访问权限:[https://docs.google.com/forms/d/1XTQc-QEjsE7YrgstyJxbFDnwmhUhBFFvpNJBw3VzuuE/viewform](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 teuunn/laravel-google-my-business

或者要使用composer安装,编辑您的composer.json以要求该包。

"require": { "teuunn/laravel-google-my-business": "1.*" }

然后在您的终端中运行 composer update 以将其拉入。

Laravel

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

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

Teuunn\LaravelGoogleMyBusiness\GoogleMyBusinessServiceProvider::class,

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

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

Google My Business Discovery文档

Google My Business API discovery文档是一个描述特定版本API表面的JSON文档。您将使用discovery文档与Google API Discovery服务一起使用。

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

提示

  • 有关Google Discovery文档的指南,请参阅:https://developers.google.com/discovery/v1/using#discovery-doc
  • 浏览这个庞大文档的有用工具是:http://jsonviewer.stack.hu/
  • 加入讨论![https://support.google.com/business/community?hl=en](https://support.google.com/business/community?hl=en)(旧平台讨论:[https://www.en.advertisercommunity.com/t5/Google-My-Business-API/bd-p/gmb-api](https://www.en.advertisercommunity.com/t5/Google-My-Business-API/bd-p/gmb-api))

代码示例

(工作中)

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

}

有用的链接

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