|
| 1 | +# Copyright 2025 Google LLC |
| 2 | +# |
| 3 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +# you may not use this file except in compliance with the License. |
| 5 | +# You may obtain a copy of the License at |
| 6 | +# |
| 7 | +# https://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +# |
| 9 | +# Unless required by applicable law or agreed to in writing, software |
| 10 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +# See the License for the specific language governing permissions and |
| 13 | +# limitations under the License. |
| 14 | + |
| 15 | + |
| 16 | +import os.path |
| 17 | + |
| 18 | +from google.auth.transport.requests import Request |
| 19 | +from google.oauth2.credentials import Credentials |
| 20 | +from google_auth_oauthlib.flow import InstalledAppFlow |
| 21 | +from googleapiclient.discovery import build |
| 22 | + |
| 23 | + |
| 24 | +# If modifying these SCOPES, delete the file token.json. |
| 25 | +SCOPES = ["https://www.googleapis.com/auth/drive.file"] |
| 26 | +CLIENT_SECRET_FILE = "client_secrets.json" |
| 27 | +TOKEN_FILE = "token.json" |
| 28 | + |
| 29 | +# TODO: Replace with your Form ID and responder's email |
| 30 | +YOUR_FORM_ID = "YOUR_FORM_ID" |
| 31 | + |
| 32 | + |
| 33 | +def get_credentials(): |
| 34 | + """Gets the credentials for the user.""" |
| 35 | + creds = None |
| 36 | + if os.path.exists(TOKEN_FILE): |
| 37 | + creds = Credentials.from_authorized_user_file(TOKEN_FILE, SCOPES) |
| 38 | + if not creds or not creds.valid: |
| 39 | + if creds and creds.expired and creds.refresh_token: |
| 40 | + creds.refresh(Request()) |
| 41 | + else: |
| 42 | + flow = InstalledAppFlow.from_client_secrets_file( |
| 43 | + CLIENT_SECRET_FILE, SCOPES |
| 44 | + ) |
| 45 | + creds = flow.run_local_server(port=8080) |
| 46 | + with open(TOKEN_FILE, "w") as token: |
| 47 | + token.write(creds.to_json()) |
| 48 | + return creds |
| 49 | + |
| 50 | + |
| 51 | +# [START forms_is_anyone_with_link_responder] |
| 52 | +def is_anyone_with_link_responder(): |
| 53 | + """Checks if anyone with the link is a responder for the form.""" |
| 54 | + creds = get_credentials() |
| 55 | + drive_service = build("drive", "v3", credentials=creds) |
| 56 | + anyone_with_link_responder = False |
| 57 | + |
| 58 | + try: |
| 59 | + permissions_result = ( |
| 60 | + drive_service.permissions() |
| 61 | + .list( |
| 62 | + fileId=YOUR_FORM_ID, |
| 63 | + fields="permissions(id,type,role,view)", |
| 64 | + includePermissionsForView="published", |
| 65 | + ) |
| 66 | + .execute() |
| 67 | + ) |
| 68 | + |
| 69 | + permissions = permissions_result.get("permissions", []) |
| 70 | + if not permissions: |
| 71 | + print(f"No permissions found for form ID: {YOUR_FORM_ID}") |
| 72 | + else: |
| 73 | + for permission in permissions: |
| 74 | + if ( |
| 75 | + permission.get("type") == "anyone" |
| 76 | + and permission.get("view") == "published" |
| 77 | + and permission.get("role") == "reader" |
| 78 | + ): |
| 79 | + anyone_with_link_responder = True |
| 80 | + break |
| 81 | + |
| 82 | + if anyone_with_link_responder: |
| 83 | + print( |
| 84 | + f"Form '{YOUR_FORM_ID}' IS configured for 'Anyone with the link' to" |
| 85 | + " respond." |
| 86 | + ) |
| 87 | + else: |
| 88 | + print( |
| 89 | + f"Form '{YOUR_FORM_ID}' is NOT configured for 'Anyone with the link'" |
| 90 | + " to respond." |
| 91 | + ) |
| 92 | + |
| 93 | + except Exception as e: |
| 94 | + print(f"An error occurred: {e}") |
| 95 | + return anyone_with_link_responder |
| 96 | + |
| 97 | + |
| 98 | +# [end forms_is_anyone_with_link_responder] |
| 99 | + |
| 100 | + |
| 101 | +# [START forms_set_anyone_with_link_responder] |
| 102 | +def set_anyone_with_link_responder(): |
| 103 | + """Sets anyone with the link to be a responder for the form.""" |
| 104 | + creds = get_credentials() |
| 105 | + drive_service = build("drive", "v3", credentials=creds) |
| 106 | + |
| 107 | + permission_body = { |
| 108 | + "type": "anyone", |
| 109 | + "view": "published", # Key for making it a responder setting |
| 110 | + "role": "reader", |
| 111 | + } |
| 112 | + |
| 113 | + try: |
| 114 | + response = ( |
| 115 | + drive_service.permissions() |
| 116 | + .create( |
| 117 | + fileId=YOUR_FORM_ID, |
| 118 | + body=permission_body, |
| 119 | + ) |
| 120 | + .execute() |
| 121 | + ) |
| 122 | + print( |
| 123 | + "'Anyone with the link can respond' permission set for form" |
| 124 | + f" {YOUR_FORM_ID}: {response}" |
| 125 | + ) |
| 126 | + return True |
| 127 | + except Exception as e: |
| 128 | + print(f"An error occurred: {e}") |
| 129 | + return False |
| 130 | + |
| 131 | + |
| 132 | +# [end forms_set_anyone_with_link_responder] |
| 133 | + |
| 134 | + |
| 135 | +# [START forms_remove_anyone_with_link_responder] |
| 136 | +def remove_anyone_with_link_responder(): |
| 137 | + """Removes anyone with the link as a responder for the form.""" |
| 138 | + creds = get_credentials() |
| 139 | + drive_service = build("drive", "v3", credentials=creds) |
| 140 | + |
| 141 | + permission_id_to_delete = None |
| 142 | + |
| 143 | + try: |
| 144 | + permissions_result = ( |
| 145 | + drive_service.permissions() |
| 146 | + .list( |
| 147 | + fileId=YOUR_FORM_ID, |
| 148 | + fields="permissions(id,type,role,view)", |
| 149 | + includePermissionsForView="published", |
| 150 | + ) |
| 151 | + .execute() |
| 152 | + ) |
| 153 | + |
| 154 | + permissions = permissions_result.get("permissions", []) |
| 155 | + for permission in permissions: |
| 156 | + if ( |
| 157 | + permission.get("type") == "anyone" |
| 158 | + and permission.get("role") == "reader" |
| 159 | + and permission.get("view") == "published" |
| 160 | + ): |
| 161 | + permission_id_to_delete = permission.get("id") |
| 162 | + break |
| 163 | + |
| 164 | + if permission_id_to_delete: |
| 165 | + drive_service.permissions().delete( |
| 166 | + fileId=YOUR_FORM_ID, permissionId=permission_id_to_delete |
| 167 | + ).execute() |
| 168 | + print( |
| 169 | + "Successfully removed 'Anyone with the link' permission (ID:" |
| 170 | + f" {permission_id_to_delete}) from form {YOUR_FORM_ID}." |
| 171 | + ) |
| 172 | + return True |
| 173 | + else: |
| 174 | + print( |
| 175 | + "'Anyone with the link can respond' permission not found for form" |
| 176 | + f" {YOUR_FORM_ID}." |
| 177 | + ) |
| 178 | + |
| 179 | + except Exception as e: |
| 180 | + print(f"An error occurred: {e}") |
| 181 | + return False |
| 182 | + |
| 183 | + |
| 184 | +# [end forms_remove_anyone_with_link_responder] |
| 185 | + |
| 186 | +if __name__ == "__main__": |
| 187 | + is_anyone_with_link_responder() |
0 commit comments