0
0

More than 1 year has passed since last update.

attributeに指定するlocationIdは、locationのlocationIdではない(GoogleMyBusiness API)

Last updated at Posted at 2022-06-23

前置き

GoogleMyBusinessのBusinessInfomationAPIでlocationを更新する際は、location_idを指定する。
locations.attributesを更新するには以下のように行う。


location_location_id = location.get("name").split("/")[-1]
attr_response = self.api.attributes.get(location_id)
attribute_location_id = attr_response["name"].split("/")[1]

attribute = gen_attribute(attribute_location_id)

# attributesAPIにpatchでリクエストするとattributesを更新できる
response = self.api.attributes.patch(location_location_id, attribute, field_mask)


attributesを更新する際に、以下のような形で指定する
スクリーンショット 2022-06-23 12.21.49.png
attributes.nameに指定するlocations/{locationId}/attributesのlocationIdは、LocationsのlocationIdではない。

attributesの中身のattributeは以下のような形
スクリーンショット 2022-06-23 12.21.39.png

# ここのlocation_idは、attribute_location_id
# attribute.getで取得したnameのIDを使う
attribute["name"] = f"""locations/{location_id}/attributes"""

# URLのvalueTypeのattributesの例
attribute["attributes"] = [
    {
        "name": "attributes/" + name,
        "valueType": "URL",
        "uriValues": uri_values # こんな感じの構造 [{ "uri": "https~~"}]
    }
]

URL以外のvalueTypeの指定方法については、attributes.listで取得した形を参考にすると良いかも

本題

locationを更新する際は、locationIDを使うが、
locationのlocationIDとattributeのlocationIDは同じものを使うわけではない。

attribute["name"] = f"""locations/{attribute_location_id}/attributes"""

attributesを更新する際のnameは、locationのlocationIdを指定するが、
attributeの要素、nameに指定するlocationIdは、attribute.getで取得したnameのLocationIdを使う

まとめ


location_location_id = location.get("name").split("/")[-1]

attr_res = self.api.attributes.get(location_location_id)

attribute_location_id = attr_res["name"].split("/")[1]

response = self.api.attributes.patch(
   location_location_id,
   gen_attribute(attribute_location_id),
   # field_maskは、attributes.nameをカンマ区切りで繋げた文字列。必須
   field_mask
)

def gen_attribute(attribute_location_id):
   return attributes = {
       #locaiton_id = location.get("name").split("/")[-1],
       "name": f"""locations/{attribute_location_id}/attributes""" 
       "attributes": [
           {
               "name": "attributes/" + attribute_name,
               # attribute_nameは"url_appointment"など文字列
               "valueType": enum (AttributeValueType),
               "values": [value],
               "repeatedEnumValue": {object (RepeatedEnumAttributeValue)},
               "uriValues": [{object (UriAttributeValue)}]
           }
       ]
   }


公式ドキュメントにはlocationIdとしか書いてないのですごく紛らわしい
スクリーンショット 2022-06-23 12.51.00.png

locationIDというと紛らわしいのでObjectIDといった方が感覚的にはわかりやすい気がする

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0