google-apioauth2-playground

Field mask error on MyBusiness Business information API


Trying to figure out how to query the My Business Business Information API to get the locations and later on the reviews. Using the Oath Playground to try this out but all attempts just gives me errors. Tried asking ChatGpt but all suggestions from that also gives error.

So trying to list the locations for a specific account:

https://mybusinessbusinessinformation.googleapis.com/v1/accounts/{accountId}/locations?fields=locations(name)

Gives me this error:

{
  "error": {
    "status": "INVALID_ARGUMENT", 
    "message": "Request contains an invalid argument.", 
    "code": 400, 
    "details": [
      {
        "fieldViolations": [
          {
            "field": "read_mask", 
            "description": "Field is required"
          }
        ], 
        "@type": "type.googleapis.com/google.rpc.BadRequest"
      }
    ]
  }
}

Also tried this request: https://mybusinessbusinessinformation.googleapis.com/v1/accounts/{accountId}/locations?read_mask=locations.name

But that gives similar error:

{
  "error": {
    "status": "INVALID_ARGUMENT", 
    "message": "Request contains an invalid argument.", 
    "code": 400, 
    "details": [
      {
        "fieldViolations": [
          {
            "field": "read_mask", 
            "description": "Invalid field mask provided"
          }
        ], 
        "@type": "type.googleapis.com/google.rpc.BadRequest"
      }
    ]
  }
}

The documentation gives nu direct hints on what to use: https://developers.google.com/my-business/reference/businessinformation/rest/v1/accounts.locations/list and getting a bit frustrated about how much time needs to be spent trying to figure Google APIs workings out.


Solution

  • I had the same issue. I found the solution in the documentation here:

    https://github.com/googleapis/google-api-php-client-services/blob/main/src/MyBusinessBusinessInformation/Resource/AccountsLocations.php

    It's very sneaky- there is an error in the documentation. readMask is a required parameter, that in the documentation is listed at the very bottom of the 'optional parameters' list...On line 83-84:

    @opt_param string readMask Required. Read mask to specify what will be returned in the response.

    So readMask is a required field. It is a string, and it should be a comma-delineated list of fields you would like to returned with the response. You can look at the Location object to get an idea of what fields you can return, or look a bit below in my example (that part's up to you, sorry):

    https://github.com/googleapis/google-api-php-client-services/blob/main/src/MyBusinessBusinessInformation/Location.php

    This won't be helpful for every use case but- for PHP, using the client library, it would look something like this:

    //  after building client...
    $business_info = new google\Service\MyBusinessBusinessInformation($client);
    
    $locations_response = $business_info->accounts_locations->listAccountsLocations($accountName, [
        'readMask' => 'labels,name,storeCode,title,websiteUri'
    ]);
    
    if(count($locations_response->locations)){
        //  do what you will here
        foreach($locations_response->locations as $location){
            //  access location object
        }
    }
    

    Hope that helps!