How to connect to Google My Business API using OAuth protocol?

Viewed 523

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.

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.

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. 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.

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

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);
1 Answers

From the error I can see you have Authentication error, make sure your client_secrets which should be a json file is correct. The following code is listing all the accounts and it is using the new Api named Account Management API. Google My Business Api will get deprecated soon.

<?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]

Related