简体   繁体   English

如何使用 OAuth 协议连接到 Google My Business API?

[英]How to connect to Google My Business API using OAuth protocol?

I am quite new to the use of Google APIs in PHP, and I am currently trying to get a list of the last Google Reviews submitted for a business.我对在 PHP 中使用 Google API 很陌生,我目前正在尝试获取为企业提交的最后一次 Google 评论的列表。

At first, I used Google Places API to get the job done, which was simple to use (no need for OAuth connection process, a simple HTTP request was enough) but I've learned that this method only returns the last 5 reviews of a business, and I need to get a list of all the reviews.一开始,我使用 Google Places API 来完成这项工作,使用起来很简单(不需要 OAuth 连接过程,一个简单的 HTTP 请求就足够了)但我了解到这种方法只返回最后 5 条评论生意,我需要得到一份所有评论的清单。

That's why I red that I needed to go with Google My Business API if I wanted get the full list of reviews for a business.这就是为什么我认为如果我想获得一个企业的完整评论列表,我需要使用 Google My Business API。 But it's been days that I am trying to get this API to work, and I still haven't managed to even connect my app to the API using OAuth protocol.但是已经有几天我试图让这个 API 工作,我仍然没有设法使用 OAuth 协议将我的应用程序连接到 API。

Though I've started from a pre-made Github example, I'm still getting this error message when trying to acess the API : PHP error message that I'm currently getting虽然我是从一个预制的 Github 示例开始的,但在尝试访问 API 时我仍然收到此错误消息:我目前收到的 PHP 错误消息

I don't understand why, as I really did everything that is mentionned in the documentation.我不明白为什么,因为我真的做了文档中提到的所有事情。

If anybody can have a little look at my code and help me knowing what is wrong with it, that'd be huge help really !如果有人可以稍微看看我的代码并帮助我了解它的问题,那将是巨大的帮助! Any piece of information would be much appreciated.任何信息将不胜感激。

Here is my code :这是我的代码:

$credentials = get_stylesheet_directory() . '/allia-google-api/google-my-business-secrets.json';

$client = new Google\Client();
$client->setAuthConfig($credentials);
$client->addScope("https://www.googleapis.com/auth/business.manage");
$redirect_uri = 'http://' . $_SERVER['HTTP_HOST'] . $_SERVER['PHP_SELF'];
$client->setRedirectUri($redirect_uri);
$my_business_account = new Google_Service_MyBusinessAccountManagement($client);

$list_accounts_response = $my_business_account->accounts->listAccounts();
var_dump($list_accounts_response);

From the error I can see you have Authentication error, make sure your client_secrets which should be a json file is correct.从错误中我可以看到你有验证错误,请确保您的CLIENT_SECRETS这应该是一个JSON文件是正确的。 The following code is listing all the accounts and it is using the new Api named Account Management API .下面的代码将列出所有帐户,它是使用名为帐户管理API新的API。 Google My Business Api will get deprecated soon.谷歌我的业务API将得到很快过时。

<?php 

include_once __DIR__ . '/vendor/autoload.php';


$credentials = __DIR__ . '/client_secrets.json';

$client = new Google\Client();
$client->setAuthConfig($credentials);
$client->addScope("https://www.googleapis.com/auth/business.manage");
$redirect_uri = 'http://' . $_SERVER['HTTP_HOST'] . $_SERVER['PHP_SELF'];
$client->setRedirectUri($redirect_uri);
$my_business_account = new Google_Service_MyBusinessAccountManagement($client);

if (isset($_GET['logout'])) { // logout: destroy token
    unset($_SESSION['token']);
  die('Logged out.');
}

if (isset($_GET['code'])) { // get auth code, get the token and store it in session
    $client->authenticate($_GET['code']);
    $_SESSION['token'] = $client->getAccessToken();
}


if (isset($_SESSION['token'])) { // get token and configure client
    $token = $_SESSION['token'];
    $client->setAccessToken($token);
}

if (!$client->getAccessToken()) { // auth call 
    $authUrl = $client->createAuthUrl();
    header("Location: ".$authUrl);
    die;
}

$list_accounts_response = $my_business_account->accounts->listAccounts();
Var_dump($list_accounts_response);

?>

check here [https://github.com/google/google-my-business-samples/tree/master/php]在这里查看 [https://github.com/google/google-my-business-samples/tree/master/php]

Thank you very much for your answer, I had already tried this method before, and yes my credentials json file is correct.非常感谢您的回答,我之前已经尝试过这种方法,是的,我的凭据 json 文件是正确的。

The problem is I don't know what the $_GET['code'] is refering too...问题是我也不知道 $_GET['code'] 指的是什么......

Therefore I can't authenticate my client, and I am always redirect to the Google Authentification error page.因此我无法验证我的客户端,我总是重定向到 Google 验证错误页面。

Do you know what this $_GET['code'] is supposed to be ?你知道这个 $_GET['code'] 应该是什么吗?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM