donglipi4495 2019-01-29 02:04
浏览 45

如何通过服务帐户身份验证获取业务位置(和评论)

I can't get the Locations list from my business under my code (PHP using the "Google APIs Client Library for PHP" together with "Google_Service_MyBusiness" Classes) when I use the "Service Account" authentication, the API returns an empty Location List.

I already have the Prerequisites and did the Basic setup, by the way, I got the information with success on OAuth Playground, under a specific AccountId, Eg: 1111111111, provided by another response there on the "OAuth Playground". (PS: I tested with my "PERSONAL" and "LOCATION_GROUP" accounts and got success with both).

But when I try to do it over my code via Server Account authentication, I can't get all the information, just the Account data that return another AccoundId, Eg: 2222222222, different of the Accounts that I got on OAuth Playground.

I did the Authentication process on OAuth Playground, using the same project where I created the "Service Account", by the way, the Permission of this "Service Account" is OWNER.

Previously, my role in my company on the Google My Business was "SITE_MANAGER", so I saw a forum answer where just "MANAGER" level/role can list the Locations, so I requested to change my permission, but continues as not the success on Locations listing.

So, I saw another Google My Business support article recommending create a "Location Group" and put my current "Location" into this group to make easy handle it, I did it and no success again.

My code is simple, based on Google guide, OAuth 2.0 for Server to Server Applications, and some Forum Questions (BTW the author's question have the same issue than me):

<?php

putenv('GOOGLE_APPLICATION_CREDENTIALS=service-account-credentials.json');

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

require_once('Google_Service_MyBusiness.php');
$mybusinessService = new Google_Service_MyBusiness($client);

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

foreach ($accountsList as $accKey => $account) {
    var_dump('$account->name', $account->name);

    $locations = $mybusinessService->accounts_locations;
    $locationsList = $locations->listAccountsLocations($account->name)->getLocations();
    var_dump('$locationsList', $locationsList);


    // Final Goal of my Code
    if (empty($locationsList)===false) {
        foreach ($locationsList as $locKey => $location) {
            $reviews = $mybusinessService->accounts_locations_reviews;
            $listReviewsResponse = $reviews->listAccountsLocationsReviews($location->name);
            $reviewsList = $listReviewsResponse->getReviews();
            var_dump('$reviewsList', $reviewsList);
        }
    }
}

I expected the Location of my business (also the reviews, but it a next step), but I just got the empty Location list.

  • 写回答

1条回答 默认 最新

  • duanduan1993 2019-01-30 19:46
    关注

    Finally, I got success using the ClientId/ClientSecret keys together with Refresh Token previously received on Google OAuth 2 Playground on the first time that I give permission to (my) App, instead "Service Account" authentication way :)

    $client = new Google_Client();
    
    $client->setClientId($clientId);
    $client->setClientSecret($clientSecret);
    
    $client->addScope("https://www.googleapis.com/auth/plus.business.manage");
    $client->setSubject('my email user on GMB');
    $client->refreshToken(' ###### ')
    

    Now I got all the needed data for my application.

    评论

报告相同问题?