首页 > 解决方案 > 更新主要类别时出现 Google My Business API 错误

问题描述

使用 Google My Business API 更新主要类别时,会显示以下错误:

+"error": {#278 ▼
    +"code": 400
    +"message": "Request contains an invalid argument."
    +"status": "INVALID_ARGUMENT"
    +"details": array:1 [▼
      0 => {#269 ▼
        +"@type": "type.googleapis.com/google.rpc.BadRequest"
        +"fieldViolations": array:1 [▼
          0 => {#275 ▼
            +"field": "update_mask"
            +"description": "primary or additional categories cannot be updated independently."
          }
        ]
      }
    ]
}

这是我的 PHP 代码:

<?php

$locationData = array();
$locationData['title'] = $data['title'];
$locationData['categories']['primaryCategory']['name'] = 'gcid:website_designer';

$updateMask = array(
    'title', 
    'categories.primaryCategory.name'
);

$queryParams = array(
    'updateMask' => implode(',', $updateMask)
);

// Prepare new cURL resource
$curl = curl_init('https://mybusinessbusinessinformation.googleapis.com/v1/locations/'.$locationId.'/?'.http_build_query($queryParams));
curl_setopt($curl, CURLOPT_CUSTOMREQUEST, 'PATCH');
curl_setopt($curl, CURLOPT_POSTFIELDS, $locationData);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_HTTPHEADER, array(
    'Content-Type: application/json',
    'Authorization: Bearer ' . $accessToken,
));

// Submit the DELETE request
$response = curl_exec($curl);

// Close cURL session handle
curl_close($curl);

$location = json_decode($response);

echo "<pre>";
print_r($location);
echo "</pre>";

这个错误是什么意思?

primary or additional categories cannot be updated independently.

当我删除此掩码时,它成功更新了公司名称

categories.primaryCategory.name

所以,我很困惑我应该添加什么掩码来更新主要类别,或者是否有单独的 API?

我基于此文档:

https://developers.google.com/my-business/reference/businessinformation/rest/v1/locations/patch

标签: phpgoogle-my-business-api

解决方案


在您的更新掩码中,您需要指定categories并提供两者。同样适用phoneNumbers


推荐阅读