Skip to content

Commit 3dc8f66

Browse files
authored
Merge pull request googleworkspace#214 from googleworkspace/forms-api-python
Initial checkin of Python Forms API sample code
2 parents 24f9c8c + 5a61599 commit 3dc8f66

9 files changed

Lines changed: 418 additions & 0 deletions

forms/snippets/README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
The Google Forms API is currently in Restricted Beta. To use the API and these
2+
samples prior to General Availability, your Google Cloud project must be
3+
allowlisted. To request that your project be allowlisted, complete the
4+
[Early Adopter Program application](https://developers.google.com/forms/api/eap).

forms/snippets/add_item.py

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
# Copyright 2021 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+
# [START forms_add_item]
16+
from __future__ import print_function
17+
from apiclient import discovery
18+
from httplib2 import Http
19+
from oauth2client import client
20+
from oauth2client import file
21+
from oauth2client import tools
22+
23+
SCOPES = "https://www.googleapis.com/auth/forms.body"
24+
DISCOVERY_DOC = "https://forms.googleapis.com/$discovery/rest?version=v1beta&key=<YOUR_API_KEY>&labels=FORMS_BETA_TESTERS"
25+
26+
store = file.Storage('credentials.json')
27+
creds = None
28+
if not creds or creds.invalid:
29+
flow = client.flow_from_clientsecrets('client_secrets.json', SCOPES)
30+
creds = tools.run_flow(flow, store)
31+
32+
form_service = discovery.build('forms', 'v1beta', http=creds.authorize(
33+
Http()), discoveryServiceUrl=DISCOVERY_DOC,static_discovery=False)
34+
35+
form = {
36+
"info": {
37+
"title": "Update item example for Forms API",
38+
}
39+
}
40+
41+
# Creates the initial Form
42+
createResult = form_service.forms().create(body=form).execute()
43+
44+
# Request body to add a video item to a Form
45+
update = {
46+
"requests": [ {
47+
"createItem": {
48+
"item": {
49+
"title": "Homework video",
50+
"description": "Quizzes in Google Forms",
51+
"videoItem": {
52+
"video": {
53+
"youtubeUri": "https://www.youtube.com/watch?v=Lt5HqPvM-eI"
54+
}
55+
}
56+
},
57+
"location": {
58+
"index": 0
59+
}
60+
}
61+
}
62+
]
63+
}
64+
65+
# Add the video to the form
66+
question_setting = form_service.forms().batchUpdate(formId=createResult["formId"], body=update).execute()
67+
68+
# Print the result to see it now has a video
69+
result = form_service.forms().get(formId=createResult["formId"]).execute()
70+
print(result)
71+
# [END forms_add_item]

forms/snippets/convert_form.py

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
# Copyright 2021 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+
# [START forms_convert_form]
16+
from __future__ import print_function
17+
from apiclient import discovery
18+
from httplib2 import Http
19+
from oauth2client import client
20+
from oauth2client import file
21+
from oauth2client import tools
22+
23+
SCOPES = "https://www.googleapis.com/auth/forms.body"
24+
DISCOVERY_DOC = "https://forms.googleapis.com/$discovery/rest?version=v1beta&key=<YOUR_API_KEY>&labels=FORMS_BETA_TESTERS"
25+
26+
store = file.Storage('credentials.json')
27+
creds = None
28+
if not creds or creds.invalid:
29+
flow = client.flow_from_clientsecrets('client_secrets.json', SCOPES)
30+
creds = tools.run_flow(flow, store)
31+
32+
form_service = discovery.build('forms', 'v1beta', http=creds.authorize(
33+
Http()), discoveryServiceUrl=DISCOVERY_DOC,static_discovery=False)
34+
35+
form = {
36+
"info": {
37+
"title": "My new form",
38+
}
39+
}
40+
41+
# Creates the initial form
42+
result = form_service.forms().create(body=form).execute()
43+
44+
# JSON to convert the form into a quiz
45+
update = {
46+
"requests": [
47+
{
48+
"updateSettings": {
49+
"settings": {
50+
"quizSettings": {
51+
"isQuiz": True
52+
}
53+
},
54+
"updateMask": "quizSettings.isQuiz"
55+
}
56+
}
57+
]
58+
}
59+
60+
# Converts the form into a quiz
61+
question_setting = form_service.forms().batchUpdate(formId=result["formId"],
62+
body=update).execute()
63+
64+
# Print the result to see it's now a quiz
65+
getresult = form_service.forms().get(formId=result["formId"]).execute()
66+
print(getresult)
67+
# [END forms_convert_form]

forms/snippets/create_form.py

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
# Copyright 2021 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+
# [START forms_create_form]
16+
from __future__ import print_function
17+
from apiclient import discovery
18+
from httplib2 import Http
19+
from oauth2client import client
20+
from oauth2client import file
21+
from oauth2client import tools
22+
23+
SCOPES = "https://www.googleapis.com/auth/drive"
24+
DISCOVERY_DOC = "https://forms.googleapis.com/$discovery/rest?version=v1beta&key=<YOUR_API_KEY>&labels=FORMS_BETA_TESTERS"
25+
26+
store = file.Storage('credentials.json')
27+
creds = None
28+
if not creds or creds.invalid:
29+
flow = client.flow_from_clientsecrets('client_secrets.json', SCOPES)
30+
creds = tools.run_flow(flow, store)
31+
32+
form_service = discovery.build('forms', 'v1beta', http=creds.authorize(
33+
Http()), discoveryServiceUrl=DISCOVERY_DOC,static_discovery=False)
34+
35+
form = {
36+
"info": {
37+
"title": "My new form",
38+
},
39+
}
40+
# Prints the details of the sample form
41+
result = form_service.forms().create(body=form).execute()
42+
print(result)
43+
# [END forms_create_form]

forms/snippets/duplicate_form.py

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
# Copyright 2021 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+
# [START forms_duplicate_form]
16+
from __future__ import print_function
17+
import os.path
18+
from googleapiclient.discovery import build
19+
from google_auth_oauthlib.flow import InstalledAppFlow
20+
from google.auth.transport.requests import Request
21+
from google.oauth2.credentials import Credentials
22+
23+
# If modifying these scopes, delete the file token.json.
24+
SCOPES = ['https://www.googleapis.com/auth/drive']
25+
26+
def main():
27+
"""Shows copy file example in Drive v3 API.
28+
Prints the name, id and other data of the copied file.
29+
"""
30+
creds = None
31+
if os.path.exists('token.json'):
32+
creds = Credentials.from_authorized_user_file('token.json', SCOPES)
33+
# If there are no (valid) credentials available, let the user log in.
34+
if not creds or not creds.valid:
35+
if creds and creds.expired and creds.refresh_token:
36+
creds.refresh(Request())
37+
else:
38+
flow = InstalledAppFlow.from_client_secrets_file(
39+
'client_secrets.json', SCOPES)
40+
creds = flow.run_local_server(port=0)
41+
# Save the credentials for the next run
42+
with open('token.json', 'w') as token:
43+
token.write(creds.to_json())
44+
45+
service = build('drive', 'v3', credentials=creds)
46+
47+
# Call the Drive v3 API
48+
origin_file_id = '1ox-6vHFeKpC6mon-tL5ygBC8zpbTnTp76JCZdIg80hA' # example ID
49+
copied_file = {'title': 'my_copy'}
50+
results = service.files().copy(
51+
fileId=origin_file_id, body=copied_file).execute()
52+
print(results)
53+
54+
if __name__ == '__main__':
55+
main()
56+
# [END forms_duplicate_form]
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
# Copyright 2021 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+
# [START forms_retrieve_all_responses]
16+
from __future__ import print_function
17+
from apiclient import discovery
18+
from httplib2 import Http
19+
from oauth2client import client
20+
from oauth2client import file
21+
from oauth2client import tools
22+
23+
SCOPES = "https://www.googleapis.com/auth/forms.responses.readonly"
24+
DISCOVERY_DOC = "https://forms.googleapis.com/$discovery/rest?version=v1beta&key=<YOUR_API_KEY>&labels=FORMS_BETA_TESTERS"
25+
26+
store = file.Storage('credentials.json')
27+
creds = None
28+
if not creds or creds.invalid:
29+
flow = client.flow_from_clientsecrets('client_secrets.json', SCOPES)
30+
creds = tools.run_flow(flow, store)
31+
service = discovery.build('forms', 'v1beta', http=creds.authorize(
32+
Http()), discoveryServiceUrl=DISCOVERY_DOC, static_discovery=False)
33+
34+
# Prints the responses of your specified form:
35+
form_id = '<YOUR_FORM_ID>'
36+
result = service.forms().responses().list(formId=form_id).execute()
37+
print(result)
38+
# [END forms_retrieve_all_responses]
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
# Copyright 2021 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+
# [START forms_retrieve_contents]
16+
from __future__ import print_function
17+
from apiclient import discovery
18+
from httplib2 import Http
19+
from oauth2client import client
20+
from oauth2client import file
21+
from oauth2client import tools
22+
23+
SCOPES = "https://www.googleapis.com/auth/forms.body.readonly"
24+
DISCOVERY_DOC = "https://forms.googleapis.com/$discovery/rest?version=v1beta&key=<YOUR_API_KEY>&labels=FORMS_BETA_TESTERS"
25+
26+
store = file.Storage('credentials.json')
27+
creds = None
28+
if not creds or creds.invalid:
29+
flow = client.flow_from_clientsecrets('client_secrets.json', SCOPES)
30+
creds = tools.run_flow(flow, store)
31+
service = discovery.build('forms', 'v1beta', http=creds.authorize(
32+
Http()), discoveryServiceUrl=DISCOVERY_DOC, static_discovery=False)
33+
34+
# Prints the title of the sample form:
35+
form_id = '<YOUR_FORM_ID>'
36+
result = service.forms().get(formId=form_id).execute()
37+
print(result)
38+
# [END forms_retrieve_contents]
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
# Copyright 2021 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+
# [START forms_retrieve_single_response]
16+
from __future__ import print_function
17+
from apiclient import discovery
18+
from httplib2 import Http
19+
from oauth2client import client
20+
from oauth2client import file
21+
from oauth2client import tools
22+
23+
SCOPES = "https://www.googleapis.com/auth/forms.responses.readonly"
24+
DISCOVERY_DOC = "https://forms.googleapis.com/$discovery/rest?version=v1beta&key=<YOUR_API_KEY>&labels=FORMS_BETA_TESTERS"
25+
26+
store = file.Storage('credentials.json')
27+
creds = None
28+
if not creds or creds.invalid:
29+
flow = client.flow_from_clientsecrets('client_secrets.json', SCOPES)
30+
creds = tools.run_flow(flow, store)
31+
service = discovery.build('forms', 'v1beta', http=creds.authorize(
32+
Http()), discoveryServiceUrl=DISCOVERY_DOC, static_discovery=False)
33+
34+
# Prints the specified response from your form:
35+
form_id = '<YOUR_FORM_ID>'
36+
response_id = '<YOUR_RESPONSE_ID>'
37+
result = service.forms().responses().get(formId=form_id,responseId=response_id).execute()
38+
print(result)
39+
# [END forms_retrieve_single_response]

0 commit comments

Comments
 (0)