如何通过Google Street View Publish API和Python正确清除街景照片所有连接?
清除Google街景照片连接失败的解决方案
问题描述
尝试使用google.streetview.publish_v1 Python客户端库清除街景照片的所有连接,但执行后连接并未被清除。以下是原代码及执行输出:
原代码
from google.streetview.publish_v1 import StreetViewPublishServiceClient from google.streetview.publish_v1 import types, enums from google.auth.transport.requests import Request from google_auth_oauthlib.flow import InstalledAppFlow import os import pickle def authenticate(): client_secrets_file = 'path_to_my_client_secrets_file.json' scopes = ['https://www.googleapis.com/auth/streetviewpublish'] credentials = None if os.path.exists('token.pickle'): with open('token.pickle', 'rb') as token: credentials = pickle.load(token) if not credentials or not credentials.valid: if credentials and credentials.expired and credentials.refresh_token: credentials.refresh(Request()) else: flow = InstalledAppFlow.from_client_secrets_file(client_secrets_file, scopes) credentials = flow.run_local_server(port=0) with open('token.pickle', 'wb') as token: pickle.dump(credentials, token) return credentials def initialize_streetview_publish_api(): credentials = authenticate() client = StreetViewPublishServiceClient(credentials=credentials) return client def clear_connections(client, photo_id): try: # Specify the view parameter photo = client.get_photo(photo_id=photo_id, view=enums.PhotoView.INCLUDE_DOWNLOAD_URL) print(f"Original connections for {photo_id}: {photo.connections}") # Clear connections photo.connections.clear() response = client.update_photo(photo=photo, update_mask=types.FieldMask(paths=['connections'])) print(f"Cleared connections for {photo_id}: {response.connections}") # Verify that the connections have been cleared updated_photo = client.get_photo(photo_id=photo_id, view=enums.PhotoView.INCLUDE_DOWNLOAD_URL) print(f"Connections after clearing for {photo_id}: {updated_photo.connections}") if not updated_photo.connections: print(f"Successfully cleared all connections for {photo_id}.") else: print(f"Failed to clear all connections for {photo_id}.") except Exception as e: print(f"Error clearing connections for {photo_id}: {e}") def main(): streetview_client = initialize_streetview_publish_api() photo_id_to_update = 'my_photo_id' # Change this to a placeholder clear_connections(streetview_client, photo_id_to_update) print(f"Deleted all connections for photo {photo_id_to_update}") if __name__ == "__main__": main()
执行输出
Original connections for my_photo_id: [target { id: "some_target_id" } , target { id: "another_target_id" } ] Cleared connections for my_photo_id: [] Connections after clearing for my_photo_id: [target { id: "some_target_id" } , target { id: "another_target_id" } ] Failed to clear all connections for my_photo_id. Deleted all connections for photo my_photo_id Process finished with exit code 0
解决方案
问题根源
直接调用photo.connections.clear()无法正确标记该字段为待更新状态。Google Street View Publish API的ProtoBuf客户端在处理列表类型字段的清空操作时,需要显式将列表设置为空列表,而非调用clear()方法,否则API无法识别到该字段的修改请求。
修改后的代码
仅需修改clear_connections函数中的连接清空逻辑:
def clear_connections(client, photo_id): try: # Specify the view parameter photo = client.get_photo(photo_id=photo_id, view=enums.PhotoView.INCLUDE_DOWNLOAD_URL) print(f"Original connections for {photo_id}: {photo.connections}") # 显式设置connections为空列表(替代clear()方法) photo.connections = [] response = client.update_photo(photo=photo, update_mask=types.FieldMask(paths=['connections'])) print(f"Cleared connections for {photo_id}: {response.connections}") # Verify that the connections have been cleared updated_photo = client.get_photo(photo_id=photo_id, view=enums.PhotoView.INCLUDE_DOWNLOAD_URL) print(f"Connections after clearing for {photo_id}: {updated_photo.connections}") if not updated_photo.connections: print(f"Successfully cleared all connections for {photo_id}.") else: print(f"Failed to clear all connections for {photo_id}.") except Exception as e: print(f"Error clearing connections for {photo_id}: {e}")
关键说明
- 将
photo.connections.clear()替换为photo.connections = [],确保ProtoBuf客户端能识别到该字段的修改,并将清空请求发送至API。 update_mask保持paths=['connections']不变,明确告知API需要更新connections字段。
修改后执行代码,即可成功清空目标街景照片的所有连接。
内容的提问来源于stack exchange,提问作者Mitko
相关产品推荐
相关产品推荐

