scottybo / laravel-google-my-business
Laravel 的一个包,实现了 Google My Business API
Requires
- php: >=7.2
- google/apiclient: ^2.0
- illuminate/support: 6.* || 7.* || 8.*
This package is not auto-updated.
Last update: 2024-09-17 04:01:00 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 scottybo/laravel-google-my-business
或者通过 composer 安装 - 修改您的 composer.json 以要求包。
"require": { "scottbo/laravel-google-my-business": "1.*" }
然后,在您的终端中运行 composer update
以将其拉入。
Laravel
重要:强烈建议您使用 https://github.com/pulkitjalan/google-apiclient 来处理 Google 客户端设置。此文档下面的代码示例假设您正在使用此库。
要将此 Google My Business 包用于 Laravel 项目,请将以下内容添加到您的 config/app.php
文件中的 providers
数组中
Scottybo\LaravelGoogleMyBusiness\GoogleMyBusinessServiceProvider::class,
接下来,将以下内容添加到您的 config/app.php
文件中的 aliases
数组中
'GoogleMyBusiness' => Scottybo\LaravelGoogleMyBusiness\GoogleMyBusiness::class
Google My Business 发现文档
Google My Business API 发现文档是一个描述 API 特定版本的 JSON 文档。您结合 Google API 发现服务使用发现文档。
mybusiness_google_rest_v4p4.json
已包含在此项目中供您参考(下载自:https://developers.google.com/my-business/samples/)
提示
- 有关 Google 发现文档的指南,请参阅:https://developers.google.com/discovery/v1/using#discovery-doc
- 浏览此庞大文档的有用工具是:http://jsonviewer.stack.hu/
- 加入讨论! https://support.google.com/business/community?hl=en(旧平台讨论: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(); } }
有用链接
主要供我在开发这个包时参考,但您也可能觉得它们很有用!
- 新社区网址: https://support.google.com/business/community?hl=en
- https://www.en.advertisercommunity.com/t5/Google-My-Business-API/Create-Post-with-My-Business-API/td-p/1704175
- https://developers.google.com/api-client-library/php/start/get_started
- https://developers.google.com/identity/protocols/OAuth2WebServer
- https://developers.google.com/identity/protocols/googlescopes
- https://developers.google.com/my-business/reference/rest/v4/accounts
- https://developers.google.com/my-business/content/get-started
- https://github.com/google/google-auth-library-php
- pulkitjalan/google-apiclient#23