namidad / laravel-google-my-business
这是一个用于Laravel的Google My Business API实现包
Requires
- php: >=7.2
- google/apiclient: ^2.0
- illuminate/support: 6.* || 7.* || 8.*|| 9.*
This package is not auto-updated.
Last update: 2024-09-30 00:04:18 UTC
README
注意:要使用Google My Business API,您必须申请访问权限: https://docs.google.com/forms/d/1XTQc-QEjsE7YrgstyJxbFDnwmhUhBFFvpNJBw3VzuuE/viewform
这是Google提供的Google My Business PHP库(v4.4)在Laravel中的实现版本,具体可参考https://developers.google.com/my-business/samples/
有关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客户端设置。下面的代码示例假设您正在使用此库。
要在Laravel项目中使用此Google My Business包,请将以下内容添加到您的 config/app.php
文件中的 providers
数组中
Scottybo\LaravelGoogleMyBusiness\GoogleMyBusinessServiceProvider::class,
然后,将以下内容添加到您的 config/app.php
文件中的 aliases
数组中
'GoogleMyBusiness' => Scottybo\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/)
提示
- 有关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