Search code examples
phpgoogle-my-business-apigoogle-my-business

Are all existing endpoints listed in documentation really still working for v4.9? (i.e. those not replaced by v1 so far)


I have tried using old v4.9 endpoints that haven't been replaced by v1 so far such as:

https://developers.google.com/my-business/reference/rest/v4/accounts.locations/reportInsights
https://developers.google.com/my-business/reference/rest/v4/accounts.locations.reviews

However, none of these endpoints work anymore.

I am using PHP client that had these endpoints missing, but using the official v4.9 library listed here: https://developers.google.com/my-business/samples/previousVersions I have been able to reach some of the old endpoints such as reviews.

However they no longer return any data or data object is empty.

Anyone has experienced similar issues?


Solution

  • The v4.9 (not yet deprecated) endpoints such as reviews, insights etc. are working, but the official library is broken and botched.

    I had to code a replacement using Guzzle client reaching to the endpoints directly instead. So you need to code the API library yourself from scratch for these v4.9 endpoints as the official library does not work.

    How to fetch reviews:

    public static function listReviews($client, $params, $account, $location)
        {
            $response = $client->authorize()->get('https://mybusiness.googleapis.com/v4/' . $account . '/' . $location . '/reviews', ['query' => $params]);
            return json_decode((string) $response->getBody(), false);
        }
    

    How to fetch insights:

    /** v4.9 working 02/2022 **/
        public static function reportInsights($client, $params, $account)
        {
            try {
                $response = $client->authorize()->post('https://mybusiness.googleapis.com/v4/' . $account . '/locations:reportInsights', [
                    \GuzzleHttp\RequestOptions::JSON => $params,
                ]);
            } catch (\GuzzleHttp\Exception\RequestException $ex) {
                return $ex->getResponse()->getBody()->getContents();
            }
            return json_decode((string) $response->getBody(), false);
        }
    

    How to prepare payload for insights:

    $params = new \stdClass();
    $params->locationNames = $account->name . '/' . $location->name;
    $time_range = new \stdClass();
    $time_range->startTime = Carbon::parse('3 days ago 00:00:00')->toISOString();
    $time_range->endTime = Carbon::parse('2 days ago 00:00:00')->toISOString();
    if ($force == 'complete') {
           $time_range->startTime = Carbon::parse('17 months ago 00:00:00')->toIso8601ZuluString();
           $time_range->endTime = Carbon::parse('3 days ago 00:00:00')->toIso8601ZuluString();
    }
    $params->basicRequest = new \stdClass();
    $params->basicRequest->timeRange = $time_range;
    $params->basicRequest->metricRequests = new \stdClass();
    $metric_request = new \stdClass();
    $metric_request->metric = 'ALL';
    $metric_request->options = ['AGGREGATED_DAILY'];
    $params->basicRequest->metricRequests = [
          $metric_request,
    ];
    

    Note: if you are getting empty insights response, you have to check verification using new v1 API call such as:

    $verifications = \Google_Service_MyBusinessVerifications($client)->locations_verifications->listLocationsVerifications($location->getName());
    $verification = '0';
    if ($verifications->getVerifications()) {
        $verification = $verifications->getVerifications()[0]->getState();
        }
    

    Using official API client with an existing token (needs to be fetched via OAuth2):

    $provider = new GoogleClientServiceProvider(true);
    $client = $provider->initializeClient($known_token, ['https://www.googleapis.com/auth/plus.business.manage', 'https://www.googleapis.com/auth/drive']);