diff --git a/forms/snippets/add_responder.py b/forms/snippets/add_responder.py new file mode 100644 index 00000000..71edbd37 --- /dev/null +++ b/forms/snippets/add_responder.py @@ -0,0 +1,86 @@ +# Copyright 2025 Google LLC +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# https://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +# [START forms_add_responder] + +import os.path + +from google.auth.transport.requests import Request +from google.oauth2.credentials import Credentials +from google_auth_oauthlib.flow import InstalledAppFlow +from googleapiclient.discovery import build + + +# If modifying these SCOPES, delete the file token.json. +SCOPES = ["https://www.googleapis.com/auth/drive.file"] +CLIENT_SECRET_FILE = "client_secrets.json" +TOKEN_FILE = "token.json" + +# TODO: Replace with your Form ID and responder's email +YOUR_FORM_ID = "YOUR_FORM_ID" +YOUR_RESPONDER_EMAIL = "user@example.com" + + +def get_credentials(): + """Gets the credentials for the user.""" + creds = None + if os.path.exists(TOKEN_FILE): + creds = Credentials.from_authorized_user_file(TOKEN_FILE, SCOPES) + if not creds or not creds.valid: + if creds and creds.expired and creds.refresh_token: + creds.refresh(Request()) + else: + flow = InstalledAppFlow.from_client_secrets_file( + CLIENT_SECRET_FILE, SCOPES + ) + creds = flow.run_local_server(port=8080) + with open(TOKEN_FILE, "w") as token: + token.write(creds.to_json()) + return creds + + +def add_responder(): + """Adds the responder to the form.""" + creds = get_credentials() + drive_service = build("drive", "v3", credentials=creds) + + permission_body = { + "view": "published", + "role": "reader", + "type": "user", + "emailAddress": YOUR_RESPONDER_EMAIL, + } + + try: + response = ( + drive_service.permissions() + .create( + fileId=YOUR_FORM_ID, + body=permission_body, + sendNotificationEmail=False, # Optional: to avoid sending an email + ) + .execute() + ) + print( + f"Added responder {YOUR_RESPONDER_EMAIL}. Permission ID:" + f" {response.get('id')}" + ) + print(response) + except Exception as e: + print(f"An error occurred: {e}") + + +if __name__ == "__main__": + add_responder() +# [end forms_add_responder] diff --git a/forms/snippets/anyone_with_link_responder.py b/forms/snippets/anyone_with_link_responder.py new file mode 100644 index 00000000..97aece4e --- /dev/null +++ b/forms/snippets/anyone_with_link_responder.py @@ -0,0 +1,187 @@ +# Copyright 2025 Google LLC +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# https://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + + +import os.path + +from google.auth.transport.requests import Request +from google.oauth2.credentials import Credentials +from google_auth_oauthlib.flow import InstalledAppFlow +from googleapiclient.discovery import build + + +# If modifying these SCOPES, delete the file token.json. +SCOPES = ["https://www.googleapis.com/auth/drive.file"] +CLIENT_SECRET_FILE = "client_secrets.json" +TOKEN_FILE = "token.json" + +# TODO: Replace with your Form ID and responder's email +YOUR_FORM_ID = "YOUR_FORM_ID" + + +def get_credentials(): + """Gets the credentials for the user.""" + creds = None + if os.path.exists(TOKEN_FILE): + creds = Credentials.from_authorized_user_file(TOKEN_FILE, SCOPES) + if not creds or not creds.valid: + if creds and creds.expired and creds.refresh_token: + creds.refresh(Request()) + else: + flow = InstalledAppFlow.from_client_secrets_file( + CLIENT_SECRET_FILE, SCOPES + ) + creds = flow.run_local_server(port=8080) + with open(TOKEN_FILE, "w") as token: + token.write(creds.to_json()) + return creds + + +# [START forms_is_anyone_with_link_responder] +def is_anyone_with_link_responder(): + """Checks if anyone with the link is a responder for the form.""" + creds = get_credentials() + drive_service = build("drive", "v3", credentials=creds) + anyone_with_link_responder = False + + try: + permissions_result = ( + drive_service.permissions() + .list( + fileId=YOUR_FORM_ID, + fields="permissions(id,type,role,view)", + includePermissionsForView="published", + ) + .execute() + ) + + permissions = permissions_result.get("permissions", []) + if not permissions: + print(f"No permissions found for form ID: {YOUR_FORM_ID}") + else: + for permission in permissions: + if ( + permission.get("type") == "anyone" + and permission.get("view") == "published" + and permission.get("role") == "reader" + ): + anyone_with_link_responder = True + break + + if anyone_with_link_responder: + print( + f"Form '{YOUR_FORM_ID}' IS configured for 'Anyone with the link' to" + " respond." + ) + else: + print( + f"Form '{YOUR_FORM_ID}' is NOT configured for 'Anyone with the link'" + " to respond." + ) + + except Exception as e: + print(f"An error occurred: {e}") + return anyone_with_link_responder + + +# [end forms_is_anyone_with_link_responder] + + +# [START forms_set_anyone_with_link_responder] +def set_anyone_with_link_responder(): + """Sets anyone with the link to be a responder for the form.""" + creds = get_credentials() + drive_service = build("drive", "v3", credentials=creds) + + permission_body = { + "type": "anyone", + "view": "published", # Key for making it a responder setting + "role": "reader", + } + + try: + response = ( + drive_service.permissions() + .create( + fileId=YOUR_FORM_ID, + body=permission_body, + ) + .execute() + ) + print( + "'Anyone with the link can respond' permission set for form" + f" {YOUR_FORM_ID}: {response}" + ) + return True + except Exception as e: + print(f"An error occurred: {e}") + return False + + +# [end forms_set_anyone_with_link_responder] + + +# [START forms_remove_anyone_with_link_responder] +def remove_anyone_with_link_responder(): + """Removes anyone with the link as a responder for the form.""" + creds = get_credentials() + drive_service = build("drive", "v3", credentials=creds) + + permission_id_to_delete = None + + try: + permissions_result = ( + drive_service.permissions() + .list( + fileId=YOUR_FORM_ID, + fields="permissions(id,type,role,view)", + includePermissionsForView="published", + ) + .execute() + ) + + permissions = permissions_result.get("permissions", []) + for permission in permissions: + if ( + permission.get("type") == "anyone" + and permission.get("role") == "reader" + and permission.get("view") == "published" + ): + permission_id_to_delete = permission.get("id") + break + + if permission_id_to_delete: + drive_service.permissions().delete( + fileId=YOUR_FORM_ID, permissionId=permission_id_to_delete + ).execute() + print( + "Successfully removed 'Anyone with the link' permission (ID:" + f" {permission_id_to_delete}) from form {YOUR_FORM_ID}." + ) + return True + else: + print( + "'Anyone with the link can respond' permission not found for form" + f" {YOUR_FORM_ID}." + ) + + except Exception as e: + print(f"An error occurred: {e}") + return False + + +# [end forms_remove_anyone_with_link_responder] + +if __name__ == "__main__": + is_anyone_with_link_responder() diff --git a/forms/snippets/get_responders.py b/forms/snippets/get_responders.py new file mode 100644 index 00000000..90849c2a --- /dev/null +++ b/forms/snippets/get_responders.py @@ -0,0 +1,91 @@ +# Copyright 2025 Google LLC +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# https://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +# [START forms_get_responders] + +import os.path + +from google.auth.transport.requests import Request +from google.oauth2.credentials import Credentials +from google_auth_oauthlib.flow import InstalledAppFlow +from googleapiclient.discovery import build + + +# If modifying these SCOPES, delete the file token.json. +SCOPES = ["https://www.googleapis.com/auth/drive.metadata.readonly"] +CLIENT_SECRET_FILE = "client_secrets.json" +TOKEN_FILE = "token.json" + +# TODO: Replace with your Form ID +YOUR_FORM_ID = "YOUR_FORM_ID" + + +def get_credentials(): + """Gets the credentials for the user.""" + creds = None + if os.path.exists(TOKEN_FILE): + creds = Credentials.from_authorized_user_file(TOKEN_FILE, SCOPES) + if not creds or not creds.valid: + if creds and creds.expired and creds.refresh_token: + creds.refresh(Request()) + else: + flow = InstalledAppFlow.from_client_secrets_file( + CLIENT_SECRET_FILE, SCOPES + ) + creds = flow.run_local_server(port=8080) + with open(TOKEN_FILE, "w") as token: + token.write(creds.to_json()) + return creds + + +def get_responders(): + """Gets the responders for the form.""" + creds = get_credentials() + drive_service = build("drive", "v3", credentials=creds) + + try: + response = ( + drive_service.permissions() + .list( + fileId=YOUR_FORM_ID, + fields="permissions(id,emailAddress,type,role,view)", + includePermissionsForView="published", + ) + .execute() + ) + + published_readers = [] + for permission in response.get("permissions", []): + # 'view': 'published' indicates it's related to the published link. + # 'role': 'reader' is standard for responders. + # 'type': 'user' for specific users, 'anyone' for public. + if ( + permission.get("view") == "published" + and permission.get("role") == "reader" + ): + published_readers.append(permission) + + if published_readers: + print("Responders for this form:") + print(published_readers) + else: + print("No specific published readers found for this form.") + + except Exception as e: + print(f"An error occurred: {e}") + + +if __name__ == "__main__": + get_responders() +# [end forms_get_responders] diff --git a/forms/snippets/publish_form.py b/forms/snippets/publish_form.py new file mode 100644 index 00000000..d8bf1de6 --- /dev/null +++ b/forms/snippets/publish_form.py @@ -0,0 +1,86 @@ +# Copyright 2025 Google LLC +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# https://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +# [START forms_publish_form] + +import os.path + +from google.auth.transport.requests import Request +from google.oauth2.credentials import Credentials +from google_auth_oauthlib.flow import InstalledAppFlow +from googleapiclient.discovery import build + + +# If modifying these SCOPES, delete the file token.json. +SCOPES = ["https://www.googleapis.com/auth/forms.body"] +DISCOVERY_DOC = "https://forms.googleapis.com/$discovery/rest?version=v1" +CLIENT_SECRET_FILE = "client_secrets.json" +TOKEN_FILE = "token.json" + +# TODO: Replace with your Form ID +YOUR_FORM_ID = "YOUR_FORM_ID" + + +def get_credentials(): + """Gets the credentials for the user.""" + creds = None + if os.path.exists(TOKEN_FILE): + creds = Credentials.from_authorized_user_file(TOKEN_FILE, SCOPES) + if not creds or not creds.valid: + if creds and creds.expired and creds.refresh_token: + creds.refresh(Request()) + else: + flow = InstalledAppFlow.from_client_secrets_file( + CLIENT_SECRET_FILE, SCOPES + ) + creds = flow.run_local_server(port=8080) + with open(TOKEN_FILE, "w") as token: + token.write(creds.to_json()) + return creds + + +def publish_form(): + """Publishes the form.""" + creds = get_credentials() + form_service = build( + "forms", + "v1", + credentials=creds, + discoveryServiceUrl=DISCOVERY_DOC, + static_discovery=False, + ) + + # Request body for updating publish settings + set_publish_settings_request = { + "publishSettings": { + "publishState": {"isPublished": True, "isAcceptingResponses": True} + }, + } + + try: + response = ( + form_service.forms() + .setPublishSettings( + formId=YOUR_FORM_ID, body=set_publish_settings_request + ) + .execute() + ) + print(f"Form {YOUR_FORM_ID} publish settings updated: {response}") + except Exception as e: + print(f"An error occurred: {e}") + + +if __name__ == "__main__": + publish_form() +# [end forms_publish_form] diff --git a/forms/snippets/remove_responder.py b/forms/snippets/remove_responder.py new file mode 100644 index 00000000..91be4494 --- /dev/null +++ b/forms/snippets/remove_responder.py @@ -0,0 +1,101 @@ +# Copyright 2025 Google LLC +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# https://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +# [START forms_remove_responder] + +import os.path + +from google.auth.transport.requests import Request +from google.oauth2.credentials import Credentials +from google_auth_oauthlib.flow import InstalledAppFlow +from googleapiclient.discovery import build + + +# If modifying these SCOPES, delete the file token.json. +SCOPES = ["https://www.googleapis.com/auth/drive.file"] +CLIENT_SECRET_FILE = "client_secrets.json" +TOKEN_FILE = "token.json" + +# TODO: Replace with your Form ID and responder's email +YOUR_FORM_ID = "YOUR_FORM_ID" +YOUR_RESPONDER_EMAIL = "user@example.com" + + +def get_credentials(): + """Gets the credentials for the user.""" + creds = None + if os.path.exists(TOKEN_FILE): + creds = Credentials.from_authorized_user_file(TOKEN_FILE, SCOPES) + if not creds or not creds.valid: + if creds and creds.expired and creds.refresh_token: + creds.refresh(Request()) + else: + flow = InstalledAppFlow.from_client_secrets_file( + CLIENT_SECRET_FILE, SCOPES + ) + creds = flow.run_local_server(port=8080) + with open(TOKEN_FILE, "w") as token: + token.write(creds.to_json()) + return creds + + +def remove_responder(): + """Removes the responder for the form.""" + creds = get_credentials() + drive_service = build("drive", "v3", credentials=creds) + + try: + # First, find the permission ID for the user + permissions_result = ( + drive_service.permissions() + .list( + fileId=YOUR_FORM_ID, + fields="permissions(id,emailAddress,role,view,type)", + includePermissionsForView="published", + ) + .execute() + ) + + permission_id_to_delete = None + for p in permissions_result.get("permissions", []): + if ( + p.get("emailAddress") == YOUR_RESPONDER_EMAIL + and p.get("role") == "reader" + and p.get("view") == "published" + and p.get("type") == "user" + ): + permission_id_to_delete = p.get("id") + break + + if permission_id_to_delete: + drive_service.permissions().delete( + fileId=YOUR_FORM_ID, permissionId=permission_id_to_delete + ).execute() + print( + f"Successfully removed responder {YOUR_RESPONDER_EMAIL} (Permission" + f" ID: {permission_id_to_delete}) from form {YOUR_FORM_ID}." + ) + else: + print( + f"Responder {YOUR_RESPONDER_EMAIL} not found or not a published" + f" reader for form {YOUR_FORM_ID}." + ) + + except Exception as e: + print(f"An error occurred: {e}") + + +if __name__ == "__main__": + remove_responder() +# [end forms_remove_responder] diff --git a/forms/snippets/stop_accepting_responses.py b/forms/snippets/stop_accepting_responses.py new file mode 100644 index 00000000..db37d00d --- /dev/null +++ b/forms/snippets/stop_accepting_responses.py @@ -0,0 +1,90 @@ +# Copyright 2025 Google LLC +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# https://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +# [START forms_stop_accepting_responses] + +import os.path + +from google.auth.transport.requests import Request +from google.oauth2.credentials import Credentials +from google_auth_oauthlib.flow import InstalledAppFlow +from googleapiclient.discovery import build + + +# If modifying these SCOPES, delete the file token.json. +SCOPES = ["https://www.googleapis.com/auth/forms.body"] +DISCOVERY_DOC = "https://forms.googleapis.com/$discovery/rest?version=v1" +CLIENT_SECRET_FILE = "client_secrets.json" +TOKEN_FILE = "token.json" + +# TODO: Replace with your Form ID +YOUR_FORM_ID = "YOUR_FORM_ID" + + +def get_credentials(): + """Gets the credentials for the user.""" + creds = None + if os.path.exists(TOKEN_FILE): + creds = Credentials.from_authorized_user_file(TOKEN_FILE, SCOPES) + if not creds or not creds.valid: + if creds and creds.expired and creds.refresh_token: + creds.refresh(Request()) + else: + flow = InstalledAppFlow.from_client_secrets_file( + CLIENT_SECRET_FILE, SCOPES + ) + creds = flow.run_local_server(port=8080) + with open(TOKEN_FILE, "w") as token: + token.write(creds.to_json()) + return creds + + +def close_form(): + """Closes the form for responses.""" + creds = get_credentials() + form_service = build( + "forms", + "v1", + credentials=creds, + discoveryServiceUrl=DISCOVERY_DOC, + static_discovery=False, + ) + + stop_accepting_request_body = { + "publishSettings": { + "publishState": { + "isPublished": True, # Keep it published + "isAcceptingResponses": False, # But stop accepting responses + } + } + } + + try: + response = ( + form_service.forms() + .setPublishSettings( # Corrected: form_service + formId=YOUR_FORM_ID, body=stop_accepting_request_body + ) + .execute() + ) + print(f"Form {YOUR_FORM_ID} stopped accepting responses: {response}") + return True + except Exception as e: + print(f"An error occurred: {e}") + return False + + +if __name__ == "__main__": + close_form() +# [end forms_stop_accepting_responses] diff --git a/forms/snippets/supports_publishing.py b/forms/snippets/supports_publishing.py new file mode 100644 index 00000000..96c7c604 --- /dev/null +++ b/forms/snippets/supports_publishing.py @@ -0,0 +1,92 @@ +# Copyright 2025 Google LLC +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# https://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +# [START forms_supports_publishing] + +import os.path + +from google.auth.transport.requests import Request +from google.oauth2.credentials import Credentials +from google_auth_oauthlib.flow import InstalledAppFlow +from googleapiclient.discovery import build + + +# If modifying these SCOPES, delete the file token.json. +SCOPES = ["https://www.googleapis.com/auth/forms.body"] +DISCOVERY_DOC = "https://forms.googleapis.com/$discovery/rest?version=v1" +CLIENT_SECRET_FILE = "client_secrets.json" +TOKEN_FILE = "token.json" + +# TODO: Replace with your Form ID +YOUR_FORM_ID = "YOUR_FORM_ID" + + +def get_credentials(): + """Gets the credentials for the user.""" + creds = None + if os.path.exists(TOKEN_FILE): + creds = Credentials.from_authorized_user_file(TOKEN_FILE, SCOPES) + if not creds or not creds.valid: + if creds and creds.expired and creds.refresh_token: + creds.refresh(Request()) + else: + flow = InstalledAppFlow.from_client_secrets_file( + CLIENT_SECRET_FILE, SCOPES + ) + creds = flow.run_local_server(port=8080) + with open(TOKEN_FILE, "w") as token: + token.write(creds.to_json()) + return creds + + +def supports_publishing(): + """Checks if the form supports publishing.""" + creds = get_credentials() + form_service = build( + "forms", + "v1", + credentials=creds, + discoveryServiceUrl=DISCOVERY_DOC, + static_discovery=False, + ) + + is_legacy = True # Assume legacy until proven otherwise + try: + form_metadata = form_service.forms().get(formId=YOUR_FORM_ID).execute() + # If 'publishSettings' field exists, it's not a legacy form regarding this feature. + if "publishSettings" in form_metadata: + print( + f"Form '{YOUR_FORM_ID}' (Title:" + f" {form_metadata.get('info', {}).get('title')}) is NOT a legacy form" + " (supports publishSettings)." + ) + is_legacy = False + else: + print( + f"Form '{YOUR_FORM_ID}' (Title:" + f" {form_metadata.get('info', {}).get('title')}) IS a legacy form" + " (does not have publishSettings field)." + ) + return not is_legacy # Returns true if it supports publishing + + except Exception as e: + print(f"An error occurred while checking form {YOUR_FORM_ID}: {e}") + # Depending on the error, it might indicate non-existence or access issues, + # not necessarily legacy status. + return None + + +if __name__ == "__main__": + supports_publishing() +# [end forms_supports_publishing] diff --git a/forms/snippets/unpublish_form.py b/forms/snippets/unpublish_form.py new file mode 100644 index 00000000..7f62e20e --- /dev/null +++ b/forms/snippets/unpublish_form.py @@ -0,0 +1,89 @@ +# Copyright 2025 Google LLC +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# https://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +# [START forms_unpublish_form] + +import os.path + +from google.auth.transport.requests import Request +from google.oauth2.credentials import Credentials +from google_auth_oauthlib.flow import InstalledAppFlow +from googleapiclient.discovery import build + + +# If modifying these SCOPES, delete the file token.json. +SCOPES = ["https://www.googleapis.com/auth/forms.body"] +DISCOVERY_DOC = "https://forms.googleapis.com/$discovery/rest?version=v1" +CLIENT_SECRET_FILE = "client_secrets.json" +TOKEN_FILE = "token.json" + +# TODO: Replace with your Form ID +YOUR_FORM_ID = "YOUR_FORM_ID" + + +def get_credentials(): + """Gets the credentials for the user.""" + creds = None + if os.path.exists(TOKEN_FILE): + creds = Credentials.from_authorized_user_file(TOKEN_FILE, SCOPES) + if not creds or not creds.valid: + if creds and creds.expired and creds.refresh_token: + creds.refresh(Request()) + else: + flow = InstalledAppFlow.from_client_secrets_file( + CLIENT_SECRET_FILE, SCOPES + ) + creds = flow.run_local_server(port=8080) + with open(TOKEN_FILE, "w") as token: + token.write(creds.to_json()) + return creds + + +def unpublish_form(): + """Unpublishes the form.""" + creds = get_credentials() + form_service = build( + "forms", + "v1", + credentials=creds, + discoveryServiceUrl=DISCOVERY_DOC, + static_discovery=False, + ) + + # Request body for updating publish settings + set_publish_settings_request = { + "publishSettings": {"publishState": {"isPublished": False}}, + } + + try: + response = ( + form_service.forms() + .setPublishSettings( + formId=YOUR_FORM_ID, body=set_publish_settings_request + ) + .execute() + ) + print( + f"Form {YOUR_FORM_ID} publish settings updated to unpublished:" + f" {response}" + ) + return True + except Exception as e: + print(f"An error occurred: {e}") + return False + + +if __name__ == "__main__": + unpublish_form() +# [end forms_unpublish_form]