AdWords is now Google Ads. Our new name reflects the full range of advertising options we offer across Search, Display, YouTube, and more. Learn more

My Business
4.5K members online now
4.5K members online now
For developers who are using the Google My Business API to manage locations
Guide Me
star_border
Reply

GMB Client Library Integration Issue

[ Edited ]
Visitor ✭ ✭ ✭
# 1
Visitor ✭ ✭ ✭

Hi Terry,

I am having some issues trying to configure the Google MyBusiness API. I am hoping you can help.

When I run the MyBusiness API code in the server, it says some class files are missing. I have downloaded the API php client library and added them to my MyBusiness page. At this point, I am not getting any response. I have created a new project and gotten the credentials from the Google API console as well as the MyBusiness API as well.

If you could check the code to tell me what I am doing wrong, it would be great. I have included classes like Google_Service, Google_Service_Resource etc, at the top of the code. I have also downloaded the Client library file from here:

https://developers.google.com/my-business/samples/

<?php
/*
* Copyright 2010 Google Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not
* use this file except in compliance with the License. You may obtain a copy of
* the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations under
* the License.
*/

/**
* Service definition for Mybusiness (v3).
*
* <p>
* The Google My Business API provides an interface for managing business
* location information on Google.</p>
*
* <p>
* For more information about this service, see the API
* <a href="https://developers.google.com/my-business/" target="_blank">Documentation</a>
* </p>
*
* @author Google, Inc.
*/
class Google_Service_Mybusiness extends Google_Service
{


public $accounts;
public $accounts_admins;
public $accounts_locations;
public $accounts_locations_admins;
public $accounts_locations_reviews;
public $attributes;
public $categories;

/**
* Constructs the internal representation of the Mybusiness service.
*
* @Param Google_Client $client
*/
public function __construct(Google_Client $client)
{
parent::__construct($client);
$this->rootUrl = 'https://mybusiness.googleapis.com/';
$this->servicePath = '';
$this->version = 'v3';
$this->serviceName = 'mybusiness';

$this->accounts = new Google_Service_Mybusiness_Accounts_Resource(
$this,
$this->serviceName,
'accounts',
array(
'methods' => array(
'deleteNotifications' => array(
'path' => 'v3/{+name}',
'httpMethod' => 'DELETE',
'parameters' => array(
'name' => array(
'location' => 'path',
'type' => 'string',
'required' => true,
),
),
),'get' => array(
'path' => 'v3/{+name}',
'httpMethod' => 'GET',
'parameters' => array(
'name' => array(
'location' => 'path',
'type' => 'string',
'required' => true,
),
),
),'getNotifications' => array(
'path' => 'v3/{+name}',
'httpMethod' => 'GET',
'parameters' => array(
'name' => array(
'location' => 'path',
'type' => 'string',
'required' => true,
),
),
),'list' => array(
'path' => 'v3/accounts',
'httpMethod' => 'GET',
'parameters' => array(
'pageToken' => array(
'location' => 'query',
'type' => 'string',
),
'pageSize' => array(
'location' => 'query',
'type' => 'integer',
),
),
),'update' => array(
'path' => 'v3/{+name}',
'httpMethod' => 'PUT',
'parameters' => array(
'name' => array(
'location' => 'path',
'type' => 'string',
'required' => true,
),
'languageCode' => array(
'location' => 'query',
'type' => 'string',
),
'validateOnly' => array(
'location' => 'query',
'type' => 'boolean',
),
),
),'updateNotifications' => array(
'path' => 'v3/{+name}',
'httpMethod' => 'PUT',
'parameters' => array(
'name' => array(
'location' => 'path',
'type' => 'string',
'required' => true,
),
),
),
)
)
);
$this->accounts_admins = new Google_Service_Mybusiness_AccountsAdmins_Resource(
$this,
$this->serviceName,
'admins',
array(
'methods' => array(
'create' => array(
'path' => 'v3/{+name}/admins',
'httpMethod' => 'POST',
'parameters' => array(
'name' => array(
'location' => 'path',
'type' => 'string',
'required' => true,
),
),
),'delete' => array(
'path' => 'v3/{+name}',
'httpMethod' => 'DELETE',
'parameters' => array(
'name' => array(
'location' => 'path',
'type' => 'string',
'required' => true,
),
),
),'list' => array(
'path' => 'v3/{+name}/admins',
'httpMethod' => 'GET',
'parameters' => array(
'name' => array(
'location' => 'path',
'type' => 'string',
'required' => true,
),
),
),
)
)
);
$this->accounts_locations = new Google_Service_Mybusiness_AccountsLocations_Resource(
$this,
$this->serviceName,
'locations',
array(
'methods' => array(
'associate' => array(
'path' => 'v3/{+name}:associate',
'httpMethod' => 'POST',
'parameters' => array(
'name' => array(
'location' => 'path',
'type' => 'string',
'required' => true,
),
),
),'batchGet' => array(
'path' => 'v3/{+name}/locations:batchGet',
'httpMethod' => 'POST',
'parameters' => array(
'name' => array(
'location' => 'path',
'type' => 'string',
'required' => true,
),
),
),'clearAssociation' => array(
'path' => 'v3/{+name}:clearAssociation',
'httpMethod' => 'POST',
'parameters' => array(
'name' => array(
'location' => 'path',
'type' => 'string',
'required' => true,
),
),
),'create' => array(
'path' => 'v3/{+name}/locations',
'httpMethod' => 'POST',
'parameters' => array(
'name' => array(
'location' => 'path',
'type' => 'string',
'required' => true,
),
'languageCode' => array(
'location' => 'query',
'type' => 'string',
),
'validateOnly' => array(
'location' => 'query',
'type' => 'boolean',
),
'requestId' => array(
'location' => 'query',
'type' => 'string',
),
),
),'delete' => array(
'path' => 'v3/{+name}',
'httpMethod' => 'DELETE',
'parameters' => array(
'name' => array(
'location' => 'path',
'type' => 'string',
'required' => true,
),
),
),'findMatches' => array(
'path' => 'v3/{+name}:findMatches',
'httpMethod' => 'POST',
'parameters' => array(
'name' => array(
'location' => 'path',
'type' => 'string',
'required' => true,
),
),
),'get' => array(
'path' => 'v3/{+name}',
'httpMethod' => 'GET',
'parameters' => array(
'name' => array(
'location' => 'path',
'type' => 'string',
'required' => true,
),
),
),'getGoogleUpdated' => array(
'path' => 'v3/{+name}:googleUpdated',
'httpMethod' => 'GET',
'parameters' => array(
'name' => array(
'location' => 'path',
'type' => 'string',
'required' => true,
),
),
),'list' => array(
'path' => 'v3/{+name}/locations',
'httpMethod' => 'GET',
'parameters' => array(
'name' => array(
'location' => 'path',
'type' => 'string',
'required' => true,
),
'filter' => array(
'location' => 'query',
'type' => 'string',
),
'pageToken' => array(
'location' => 'query',
'type' => 'string',
),
'pageSize' => array(
'location' => 'query',
'type' => 'integer',
),
),
),'patch' => array(
'path' => 'v3/{+name}',
'httpMethod' => 'PATCH',
'parameters' => array(
'name' => array(
'location' => 'path',
'type' => 'string',
'required' => true,
),
'languageCode' => array(
'location' => 'query',
'type' => 'string',
),
'validateOnly' => array(
'location' => 'query',
'type' => 'boolean',
),
'fieldMask' => array(
'location' => 'query',
'type' => 'string',
),
),
),'transfer' => array(
'path' => 'v3/{+name}:transfer',
'httpMethod' => 'POST',
'parameters' => array(
'name' => array(
'location' => 'path',
'type' => 'string',
'required' => true,
),
),
),
)
)
);
$this->accounts_locations_admins = new Google_Service_Mybusiness_AccountsLocationsAdmins_Resource(
$this,
$this->serviceName,
'admins',
array(
'methods' => array(
'create' => array(
'path' => 'v3/{+name}/admins',
'httpMethod' => 'POST',
'parameters' => array(
'name' => array(
'location' => 'path',
'type' => 'string',
'required' => true,
),
),
),'delete' => array(
'path' => 'v3/{+name}',
'httpMethod' => 'DELETE',
'parameters' => array(
'name' => array(
'location' => 'path',
'type' => 'string',
'required' => true,
),
),
),'list' => array(
'path' => 'v3/{+name}/admins',
'httpMethod' => 'GET',
'parameters' => array(
'name' => array(
'location' => 'path',
'type' => 'string',
'required' => true,
),
),
),
)
)
);
$this->accounts_locations_reviews = new Google_Service_Mybusiness_AccountsLocationsReviews_Resource(
$this,
$this->serviceName,
'reviews',
array(
'methods' => array(
'deleteReply' => array(
'path' => 'v3/{+name}/reply',
'httpMethod' => 'DELETE',
'parameters' => array(
'name' => array(
'location' => 'path',
'type' => 'string',
'required' => true,
),
),
),'get' => array(
'path' => 'v3/{+name}',
'httpMethod' => 'GET',
'parameters' => array(
'name' => array(
'location' => 'path',
'type' => 'string',
'required' => true,
),
),
),'list' => array(
'path' => 'v3/{+name}/reviews',
'httpMethod' => 'GET',
'parameters' => array(
'name' => array(
'location' => 'path',
'type' => 'string',
'required' => true,
),
'orderBy' => array(
'location' => 'query',
'type' => 'string',
),
'pageToken' => array(
'location' => 'query',
'type' => 'string',
),
'pageSize' => array(
'location' => 'query',
'type' => 'integer',
),
),
),'reply' => array(
'path' => 'v3/{+name}/reply',
'httpMethod' => 'POST',
'parameters' => array(
'name' => array(
'location' => 'path',
'type' => 'string',
'required' => true,
),
),
),
)
)
);
$this->attributes = new Google_Service_Mybusiness_Attributes_Resource(
$this,
$this->serviceName,
'attributes',
array(
'methods' => array(
'list' => array(
'path' => 'v3/attributes',
'httpMethod' => 'GET',
'parameters' => array(
'languageCode' => array(
'location' => 'query',
'type' => 'string',
),
'name' => array(
'location' => 'query',
'type' => 'string',
),
'pageSize' => array(
'location' => 'query',
'type' => 'integer',
),
'country' => array(
'location' => 'query',
'type' => 'string',
),
'pageToken' => array(
'location' => 'query',
'type' => 'string',
),
'categoryId' => array(
'location' => 'query',
'type' => 'string',
),
),
),
)
)
);
$this->categories = new Google_Service_Mybusiness_Categories_Resource(
$this,
$this->serviceName,
'categories',
array(
'methods' => array(
'list' => array(
'path' => 'v3/categories',
'httpMethod' => 'GET',
'parameters' => array(
'searchTerm' => array(
'location' => 'query',
'type' => 'string',
),
'regionCode' => array(
'location' => 'query',
'type' => 'string',
),
'pageToken' => array(
'location' => 'query',
'type' => 'string',
),
'pageSize' => array(
'location' => 'query',
'type' => 'integer',
),
'languageCode' => array(
'location' => 'query',
'type' => 'string',
),
),
),
)
)
);
}
}

 

Included only the half of the code since have more number of lines.

I have tried to install the google php client library using manuall and also using composer but I get the error as autoload and other services are not found or blank page.

Kindly assist me to retrieve the locations and its data.

Thank you.

1 Expert replyverified_user

Re: GMB Client Library Integration Issue

Google Employee
# 2
Google Employee

Hi @Todd S,

 

First of all, you should make sure that you installed the v1-branch of the repo for the Google API Client Library for PHP on GitHub or via Composer as instructed on the Installation page. After obtaining the library files, you should include the autoloader in your application. I notice that you included the source code of the PHP client library for the Google My Business API in your application from your first post of this topic. You should just use the include statement to include and evaluate the specified "Mybusiness.php" file.

 

Please follow the code snippet below for using an OAuth 2.0 client ID for OAuth 2.0 authorization and persisting the refresh token with the Google API Client Library for PHP (Check out this thread for more help):

require_once '/path/to/google-api-php-client/vendor/autoload.php';

include_once "Mybusiness.php";

 

define('APPLICATION_NAME', 'User Query - Google My Business API');

define('CREDENTIALS_PATH', '/path/to/credentials.json');

define('CLIENT_SECRET_PATH', '/path/to/client_secrets.json');

 

$redirect_uri = '<YOUR_REDIRECT_URI>';

 

$client = new Google_Client();

$client->setApplicationName(APPLICATION_NAME);

$client->setAuthConfigFile(CLIENT_SECRET_PATH);

$client->addScope("https://www.googleapis.com/auth/plus.business.manage");

$client->setRedirectUri($redirect_uri);

 

// For retrieving the refresh token

$client->setAccessType("offline");

$client->setApprovalPrompt("force");

 

/************************************************

We are going to create the Google My Business API

service, and query it.

************************************************/

$mybusinessService = new Google_Service_Mybusiness($client);

 

$credentialsPath = CREDENTIALS_PATH;

if (isset($_GET['code'])) {

  // Exchange authorization code for an access token.

  $accessToken = $client->authenticate($_GET['code']);

  // Store the credentials to disk.

  if (!file_exists(dirname($credentialsPath))) {

    mkdir(dirname($credentialsPath), 0700, true);

  }

  file_put_contents($credentialsPath, $accessToken);

  $redirect = 'http://' . $_SERVER['HTTP_HOST'] . $_SERVER['PHP_SELF'];

  header('Location: ' . filter_var($redirect, FILTER_SANITIZE_URL));

}

 

// Load previously authorized credentials from a file.

if (file_exists($credentialsPath)) {

  $accessToken = file_get_contents($credentialsPath);

  $client->setAccessToken($accessToken);

  // Refresh the token if it's expired.

  if ($client->isAccessTokenExpired()) {

    $client->refreshToken($client->getRefreshToken());

    file_put_contents($credentialsPath, $client->getAccessToken());

  }

} else {

  // Request authorization from the user.

  $authUrl = $client->createAuthUrl();

}

 

// For testing purposes, selects the very first account in the accounts array

$accounts = $mybusinessService->accounts;

$accountsList = $accounts->listAccounts()->getAccounts();

$account = $accountsList[0];

 

// For testing purposes, selects the very first location in the locations array

$locations = $mybusinessService->accounts_locations;

$locationsList = $locations->listAccountsLocations($account->name)->getLocations();

$location = $locationsList[0];

 

// Lists all reviews for the specified location

$reviews = $mybusinessService->accounts_locations_reviews;

$listReviewsResponse = $reviews->listAccountsLocationsReviews($location->name);

$reviewsList = $listReviewsResponse->getReviews();

print '<pre>' . json_encode($reviewsList, JSON_PRETTY_PRINT) . '</pre>';

print '<pre>' . json_encode($listReviewsResponse, JSON_PRETTY_PRINT) . '</pre>';

 

Please also check out this Accepted Solution for setting up a service account with v1-branch of the Google API Client Library for PHP and the PHP client library.

 

The PHP client library doesn’t currently support the display of detailed error messages. Please follow the Make a simple HTTP request step-by-step guide on the Google Developers site to learn how to use the OAuth 2.0 Playground for troubleshooting errors with detailed error messages when sending requests with data via the Google My Business API.

 

There are plenty of resources for using the PHP client library on this board. When you search in the Google My Business API community for the answer, please use the search box at the top of the page and filter your search results “By location” and select the “Google My Business API” board. This will limit your search results to the topics created in the Google My Business API board only. You can click on “Advanced Search…” to the right of the search box on the search results page to view results by “Specific posts” for the answer found in posts under a topic.

 

You can learn about how to list all paginated locations by including the pageSize and pageToken query parameters with the PHP client library in this thread.

 

You can learn about how to create a new location with the PHP client library in this thread.

 

You can learn about how to update a location with the PHP client library in this thread.

 

Thanks,

Terry

GMB Client Library Integration Issue

[ Edited ]
Visitor ✭ ✭ ✭
# 3
Visitor ✭ ✭ ✭

Hi Terry,

I have done the points you mentioned in the previous posts.

Here is the code..

<?php
/*
 * Copyright 2011 Google Inc.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

// include your composer dependencies
require_once 'vendor/autoload.php';

include_once 'Mybusiness.php';

define('APPLICATION_NAME', 'User Query - Google My Business API');
define('CREDENTIALS_PATH', '/path/to/credentials.json');
define('CLIENT_SECRET_PATH', __DIR__ .'/client_secrets.json'); // included the o auth client json here. downloaded from google api console

$redirect_uri = 'http://localhost/mybusinessapi/';

$client = new Google_Client();

$client->setApplicationName('mybusiness'); // APPLICATION_NAME
$client->setAuthConfigFile('Y< Text has been removed to protect private information >O'); // CLIENT_SECRET_PATH
$client->addScope("https://www.googleapis.com/auth/plus.business.manage");
$client->setRedirectUri($redirect_uri);

// For retrieving the refresh token
$client->setAccessType("offline");
$client->setApprovalPrompt("force");

/************************************************
We are going to create the Google My Business API
service, and query it.
************************************************/
$mybusinessService = new Google_Service_Mybusiness($client);
$credentialsPath = CREDENTIALS_PATH;
if (isset($_GET['code'])) {
  // Exchange authorization code for an access token.
  $accessToken = $client->authenticate($_GET['code']);
  // Store the credentials to disk.
  if (!file_exists(dirname($credentialsPath))) {
    mkdir(dirname($credentialsPath), 0700, true);
  }
  file_put_contents($credentialsPath, $accessToken);
  $redirect = 'http://' . $_SERVER['HTTP_HOST'] . $_SERVER['PHP_SELF'];
  header('Location: ' . filter_var($redirect, FILTER_SANITIZE_URL));
}

// Load previously authorized credentials from a file.
if (file_exists($credentialsPath)) {
  $accessToken = file_get_contents($credentialsPath);
  $client->setAccessToken($accessToken);
  // Refresh the token if it's expired.
  if ($client->isAccessTokenExpired()) {
    $client->refreshToken($client->getRefreshToken());
    file_put_contents($credentialsPath, $client->getAccessToken());
  }
} else {
  // Request authorization from the user.
  $authUrl = $client->createAuthUrl();
}

// For testing purposes, selects the very first account in the accounts array
$accounts = $mybusinessService->accounts;
$accountsList = $accounts->listAccounts()->getAccounts();
$account = $accountsList[0];

// For testing purposes, selects the very first location in the locations array
$locations = $mybusinessService->accounts_locations;
$locationsList = $locations->listAccountsLocations($account->name)->getLocations();
$location = $locationsList[0];

// Lists all reviews for the specified location
$reviews = $mybusinessService->accounts_locations_reviews;
$listReviewsResponse = $reviews->listAccountsLocationsReviews($location->name);
$reviewsList = $listReviewsResponse->getReviews();
print '<pre>' . json_encode($reviewsList, JSON_PRETTY_PRINT) . '</pre>';
print '<pre>' . json_encode($listReviewsResponse, JSON_PRETTY_PRINT) . '</pre>';
?>


What is the credentials path I need to define in this line

define('CREDENTIALS_PATH', '/path/to/credentials.json');


I got this error when I run the code:

Fatal error: Uncaught exception 'InvalidArgumentException' with message 'file does not exist' in C:\xampp\htdocs\mybusinessapi\vendor\google\apiclient\src\Google\Client.php:839 Stack trace: #0 C:\xampp\htdocs\mybusinessapi\vendor\google\apiclient\src\Google\Client.php(824): Google_Client->setAuthConfig('Y< Text has been removed to protect private information >...') #1 C:\xampp\htdocs\mybusinessapi\index.php(32): Google_Client->setAuthConfigFile('YtsgUgPQbMxaO81...') #2 {main} thrown in C:\xampp\htdocs\mybusinessapi\vendor\google\apiclient\src\Google\Client.php on line 839

Kindly assist me in this.

Thank you.

Re: GMB Client Library Integration Issue

Google Employee
# 4
Google Employee

Hi @Todd S,

 

Your error is caused by the line of code below:

$client->setAuthConfigFile('Y< Text has been removed to protect private information >O'); // CLIENT_SECRET_PATH

 

Please keep the PHP named constant CLIENT_SECRET_PATH intact and do not put your client ID or client secret in this line of code:

$client->setAuthConfigFile(CLIENT_SECRET_PATH);

 

The CLIENT_SECRET_PATH constant should specify the path of your client_secrets.json file which you set correctly:

define('CLIENT_SECRET_PATH', __DIR__ .'/client_secrets.json'); // included the o auth client json here. downloaded from google api console

 

Please also specify the path of the credentials file for storing your access token and refresh token in the line of code below:

define('CREDENTIALS_PATH', '/path/to/credentials.json');

 

The CREDENTIALS_PATH constant should be the path of a file for storing the obtained access token and refresh token so that the application will have the refresh token for future use to obtain a new access token when the it expires.

 

I hope this fixes your issue.

 

Thanks,

Terry

GMB Client Library Integration Issue

Visitor ✭ ✭ ✭
# 5
Visitor ✭ ✭ ✭

Hi Terry,

 

I have done the changes you mentioned.

 

Here is my code,

 

<?php
/*
 * Copyright 2011 Google Inc.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

// include your composer dependencies
require_once 'vendor/autoload.php';

include_once 'Mybusiness.php';

define('APPLICATION_NAME', 'User Query - Google My Business API');
define('CREDENTIALS_PATH', __DIR__ .'/credentials.json');
define('CLIENT_SECRET_PATH', __DIR__ .'/client_secrets.json'); // included the o auth client json here. downloaded from google api console

$redirect_uri = 'http://localhost/mybusinessapi/';

$client = new Google_Client();

$client->setApplicationName(APPLICATION_NAME); // APPLICATION_NAME
$client->setAuthConfigFile(CLIENT_SECRET_PATH); // CLIENT_SECRET_PATH
$client->addScope("https://www.googleapis.com/auth/plus.business.manage");
$client->setRedirectUri($redirect_uri);

// For retrieving the refresh token
$client->setAccessType("offline");
$client->setApprovalPrompt("force");

/************************************************
We are going to create the Google My Business API
service, and query it.
************************************************/
$mybusinessService = new Google_Service_Mybusiness($client);
$credentialsPath = CREDENTIALS_PATH;
if (isset($_GET['code'])) {
  // Exchange authorization code for an access token.
  $accessToken = $client->authenticate($_GET['code']);
  // Store the credentials to disk.
  if (!file_exists(dirname($credentialsPath))) {
    mkdir(dirname($credentialsPath), 0700, true);
  }
  file_put_contents($credentialsPath, $accessToken);
  $redirect = 'http://' . $_SERVER['HTTP_HOST'] . $_SERVER['PHP_SELF'];
  header('Location: ' . filter_var($redirect, FILTER_SANITIZE_URL));
}

// Load previously authorized credentials from a file.
if (file_exists($credentialsPath)) {
  $accessToken = file_get_contents($credentialsPath);
  $client->setAccessToken($accessToken);
  // Refresh the token if it's expired.
  if ($client->isAccessTokenExpired()) {
    $client->refreshToken($client->getRefreshToken());
    file_put_contents($credentialsPath, $client->getAccessToken());
  }
} else {
  // Request authorization from the user.
  $authUrl = $client->createAuthUrl();
}

// For testing purposes, selects the very first account in the accounts array
$accounts = $mybusinessService->accounts;
$accountsList = $accounts->listAccounts()->getAccounts();
$account = $accountsList[0];

// For testing purposes, selects the very first location in the locations array
$locations = $mybusinessService->accounts_locations;
$locationsList = $locations->listAccountsLocations($account->name)->getLocations();
$location = $locationsList[0];

// Lists all reviews for the specified location
$reviews = $mybusinessService->accounts_locations_reviews;
$listReviewsResponse = $reviews->listAccountsLocationsReviews($location->name);
$reviewsList = $listReviewsResponse->getReviews();
print '<pre>' . json_encode($reviewsList, JSON_PRETTY_PRINT) . '</pre>';
print '<pre>' . json_encode($listReviewsResponse, JSON_PRETTY_PRINT) . '</pre>';
?>

I have created the empty json file for credentials.json and included in this line
define('CREDENTIALS_PATH', __DIR__ .'/credentials.json');

 

Now I got this error message.

 


Notice: Undefined index: expires_in in C:\xampp\htdocs\mybusinessapi\vendor\google\apiclient\src\Google\Client.php on line 470

Fatal error: Uncaught exception 'LogicException' with message 'refresh token must be passed in or set as part of setAccessToken' in C:\xampp\htdocs\mybusinessapi\vendor\google\apiclient\src\Google\Client.php:258 Stack trace: #0 C:\xampp\htdocs\mybusinessapi\vendor\google\apiclient\src\Google\Client.php(246): Google_Client->fetchAccessTokenWithRefreshToken(NULL) #1 C:\xampp\htdocs\mybusinessapi\index.php(64): Google_Client->refreshToken(NULL) #2 {main} thrown in C:\xampp\htdocs\mybusinessapi\vendor\google\apiclient\src\Google\Client.php on line 258


Kindly assist me to get rid of this.

 

Thank you.

Re: GMB Client Library Integration Issue

Google Employee
# 6
Google Employee

Hi @Todd S,

 

Please simply remove the credentials file in your file system specified in the line of code below if it exists but doesn't contain a valid access token and a valid refresh token:

define('CREDENTIALS_PATH', __DIR__ .'/credentials.json');

 

After you've applied the above step, your issue should be resolved.

 

Thanks,

Terry

GMB Client Library Integration Issue

Visitor ✭ ✭ ✭
# 7
Visitor ✭ ✭ ✭

Hi Terry,

 

I have deleted the credentials.json file and leave the code as it was like

define('CREDENTIALS_PATH', __DIR__ .'/credentials.json');

now it throws me this error

Fatal error: Uncaught exception 'Google_Service_Exception' with message '{ "error": { "code": 401, "message": "The request does not have valid authentication credentials.", "errors": [ { "message": "The request does not have valid authentication credentials.", "domain": "global", "reason": "unauthorized" } ], "status": "UNAUTHENTICATED" } } ' in C:\xampp\htdocs\mybusinessapi\vendor\google\apiclient\src\Google\Http\REST.php:118 Stack trace: #0 C:\xampp\htdocs\mybusinessapi\vendor\google\apiclient\src\Google\Http\REST.php(94): Google_Http_REST::decodeHttpResponse(Object(GuzzleHttp\Psr7\Response), Object(GuzzleHttp\Psr7\Request), 'Google_Service_...') #1 [internal function]: Google_Http_REST::doExecute(Object(GuzzleHttp\Client), Object(GuzzleHttp\Psr7\Request), 'Google_Service_...') #2 C:\xampp\htdocs\mybusinessapi\vendor\google\apiclient\src\Google\Task\Runner.php(181): call_user_func_array(Array, Array) #3 C:\xampp\htdocs\mybusinessapi\vendor\google\apiclient\src\ in C:\xampp\htdocs\mybusinessapi\vendor\google\apiclient\src\Google\Http\REST.php on line 118

Thank you.

Re: GMB Client Library Integration Issue

[ Edited ]
Google Employee
# 8
Google Employee

Hi @Todd S,

 

I see that your redirect URI is set to a directory and not a file on localhost:

$redirect_uri = 'http://localhost/mybusinessapi/';

 

Did you choose, set and check the correct redirect URI (e.g. http://localhost/mybusinessapi/<YOUR_CURRENT_SCRIPT_FILENAME>) to request user consent for your web server application to work? Were you prompted the consent screen in a browser window for you to manually authorize the application? When you were creating an OAuth 2.0 client ID through the Google API Console, did you specify the correct application type (My code snippet is for “Web application”). Can you please try to troubleshoot further and see if you can fix this issue on your own?

 

Please note that the $_GET['code'] variable is a PHP variable in an associative array passed to the current script in the code snippet via the URL parameter in the query string that has the code field for an authorization code that your application can use to obtain an access token. The webpage where the $_GET['code'] variable is returned when Google sends responses to your authentication requests is determined by the redirect URI that you set in the Google API Console.

 

For simplicity, I suggest you set the redirect URI to the URL of your current script if you are using my code snippet. If you set your redirect URI to the URL of a separate webpage, you can use the following code to redirect to the current script with the query string containing the returned $_GET['code'] variable for an authorization code:

header('Location: <YOUR_CURRENT_SCRIPT_URL>?' . $_SERVER['QUERY_STRING']);

 

Using the Google My Business API effectively requires technical savvy and coding knowledge. You should have a basic knowledge of RESTful APIs. I suggest you revisit and study the Prerequisites of the Google My Business API and the Basic steps of Using OAuth 2.0 to Access Google APIs to learn how to use OAuth 2.0 authentication and authorization and make requests via the Google My Business API. Please also study the Authentication & Authorization Overview to learn how to implement the OAuth 2.0 protocol for different types of applications with the Google API Client Library for PHP.

 

Prior to coding your application, I strongly recommend you follow the Make a simple HTTP request step-by-step guide on the Google Developers site to learn how to use the OAuth 2.0 Playground for troubleshooting errors with detailed error messages when sending requests with data via the Google My Business API since the PHP client library doesn’t currently support the display of detailed error messages.

 

Please let me know if you can’t seem to figure out the solution for your issue.

 

Thanks,

Terry

GMB Client Library Integration Issue

Visitor ✭ ✭ ✭
# 9
Visitor ✭ ✭ ✭

Hi Terry,

 

I have run the oauth playground for mybusiness API

This returns the following as the output:

can you tell me how can I verify the account and get the authorized access.

 

GET /v3/accounts HTTP/1.1
Host: mybusiness.googleapis.com
Content-length: 0
Authorization: Bearer ya29.Ci95A76WTDoEDbGtt8KzYSC3aPjyTRjbJqwL5U5lNCnIQhNKAb2p2ByAtU7n_RWDMg
 
HTTP/1.1 200 OK
Content-length: 203
X-xss-protection: 1; mode=block
Content-location: https://mybusiness.googleapis.com/v3/accounts
X-content-type-options: nosniff
Transfer-encoding: chunked
Vary: Origin, X-Origin, Referer
Server: ESF
-content-encoding: gzip
Cache-control: private
Date: Tue, 11 Oct 2016 20:45:19 GMT
X-frame-options: SAMEORIGIN
Alt-svc: quic=":443"; ma=2592000; v="36,35,34,33,32"
Content-type: application/json; charset=UTF-8
{
 
"accounts": [
   
{
     
"state": {
       
"status": "UNVERIFIED"
     
},
     
"type": "PERSONAL",
     
"name": "accounts/111717770017462975110",
     
"accountName": "Todd Sobrado"
   
}
 
]
}

It shows the status as unverfied.

Kindly reply to this to make it works well. Thank you.

 

 

GMB Client Library Integration Issue

Visitor ✭ ✭ ✭
# 10
Visitor ✭ ✭ ✭

Hi Terry,

 

From the previous post you are asking to modify the redirect url to point to the file not the directory.

 

I have set the redirect url to be the same page ex: Index page of the web application.

 

And can you tell me what query string I need to pass along with the redirect url to get the $_GET['code'] query string variable.

 

Kindly assist me in this.

 

Thank you.