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

Google My business Rest API Method

Visitor ✭ ✭ ✭
# 1
Visitor ✭ ✭ ✭

Hi Terry,

 

I have followed this method to get the account in google my business. I am using xampp server and using localhost.

 

My code is:

 

<?php

$hdrarray=array('https://www.googleapis.com/auth/plus.business.manage');

$ch = curl_init();
$url = 'https://mybusiness.googleapis.com/v3/accounts;

$ch = curl_init($url);
curl_setopt($ch, CURLOPT_HTTPHEADER, $hdrarray);
curl_setopt($ch, CURLOPT_TIMEOUT, 5);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 5);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$datas = curl_exec($ch);
curl_close($ch);
$objarrs = json_decode($datas);

echo '<pre>';
print_r($objarrs);
echo '</pre>';

?>

 

This code return this error message:

 

stdClass Object
(
    [error] => stdClass Object
        (
            [code] => 401
            [message] => The request does not have valid authentication credentials.
            [status] => UNAUTHENTICATED
        )

)

 

After that I have changed my code to get the particular account details using the below code

 

<?php

$hdrarray=array('https://www.googleapis.com/auth/plus.business.manage');

$ch = curl_init();
$cust_id = "111717770017462975110";
$url = 'https://mybusiness.googleapis.com/v3/'.$cust_id;

$ch = curl_init($url);
curl_setopt($ch, CURLOPT_HTTPHEADER, $hdrarray);
curl_setopt($ch, CURLOPT_TIMEOUT, 5);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 5);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$datas = curl_exec($ch);
curl_close($ch);
$objarrs = json_decode($datas);

echo '<pre>';
print_r($objarrs);
echo '</pre>';

?>

 

This returns the empty screen.

 

Kindly assist me to get the all accounts data and the location data.

 

Thank you.

1 Expert replyverified_user

Re: Google My business Rest API Method

Google Employee
# 2
Google Employee

Hi @Todd S,

 

Your scope parameter shouldn’t be set in a HTTP request header field in your PHP cURL session. If you want to implement your own method to send an authentication request to Google, you should understand Google's OAuth 2.0 implementation for authentication, which conforms to the OpenID Connect specification.

 

Please note that you should make HTTP requests by sending an obtained access token to the Google My Business API in an HTTP authorization header after you request user consent and authorize your app. Otherwise, you will always encounter the 401 Unauthorized error.

 

In addition, you should make sure that the URIs used for each supported operation in the API is correct as summarized in the Google My Business API Reference document. You supplied a wrong GET request URI for getting the specified account in your second PHP cURL session. The correct GET request URI with 123456 as the sample account_name for getting the specified account should be the following:

https://mybusiness.googleapis.com/v3/accounts/123456

 

We strongly encourage you to use the client libraries that we provide when interacting with Google's OAuth 2.0 endpoints for the security implications of getting the implementation correct.

 

I suggest you follow the steps in the tutorial I provided to you in this thread to set up the PHP client library and list all accounts for the authenticated user via the Google My Business API.

 

Thanks,

Terry