前提
*前回質問させて頂いた「配列を特定のキー,階層にした状態で、JSON形式に変換したい」に関連する質問です。宜しければご覧下さい
https://teratail.com/questions/hztqqe08vcxwai
Google Forms APIをPython経由で使用しています。
事前に出力したJSONファイルを、Google Forms APIのKey「ChoiceQuestion.option」に反映しようとしたところ、エラーが発生し、フォームに反映させることが出来ませんでした。
実現したいこと
- 用意したJSONファイルの値を、値「choiceQuestion.options」に反映できるようにする
発生している問題・エラーメッセージ
<HttpError 400 when requesting https://forms.googleapis.com/v1/forms/**********:batchUpdate?alt=json returned "ChoiceQuestion.options cannot contain duplicate values". Details: "ChoiceQuestion.options cannot contain duplicate values"> File "F:\Python\Google Forms API\FAURS2023\updateForms.py", line 78, in <module> formId=FORM_ID, body=update).execute() ^^^^^^^^^ googleapiclient.errors.HttpError: <HttpError 400 when requesting https://forms.googleapis.com/v1/forms/**********:batchUpdate?alt=json returned "ChoiceQuestion.options cannot contain duplicate values". Details: "ChoiceQuestion.options cannot contain duplicate values">
該当のソースコード・データ
入力するJSONファイル
JSON
1// utf-8で出力済 2[ 3 { 4 "value": "['あいうえお'](['aiueo'])", 5 "image": { 6 "sourceUri": "['https://*****']" 7 } 8 }, 9 // 以下、同様のキー,値が続く 10]
ソースコード
py
1# 初期化 2from __future__ import print_function 3 4from apiclient import discovery 5from httplib2 import Http 6from oauth2client import client, file, tools 7 8import json 9 10SCOPES = "https://www.googleapis.com/auth/drive" 11DISCOVERY_DOC = "https://forms.googleapis.com/$discovery/rest?version=v1" 12FORM_ID = "**********" 13AVATARSINFO_JSON = open('avatarsInfo.json', 'r', encoding="utf-8") 14 15# 認証情報トークン 16store = file.Storage('token.json') 17creds = None 18if not creds or creds.invalid: 19 flow = client.flow_from_clientsecrets('client_secrets.json', SCOPES) 20 creds = tools.run_flow(flow, store) 21 22form_service = discovery.build('forms', 'v1', http=creds.authorize( 23 Http()), discoveryServiceUrl=DISCOVERY_DOC, static_discovery=False) 24 25json_dict = json.load(AVATARSINFO_JSON) 26 27update = { 28 "includeFormInResponse": True, 29 "requests": [{ 30 "updateFormInfo": { 31 "info": { 32 "title": "SurveyForm", 33 "description": "調査フォームです。" 34 }, 35 "updateMask": "title,description" 36 } 37 }], 38 "requests": [{ 39 "updateItem": { 40 "item": { 41 "title": "テスト質問文", 42 "description": "テスト質問文概要", 43 "questionItem": { 44 "question": { 45 #エラー発生 46 "choiceQuestion": { 47 "type": "CHECKBOX", 48 "options": [ 49 json_dict 50 ] 51 } 52 } 53 } 54 }, 55 "location": { 56 "index": 0 57 }, 58 "updateMask": "title,description,questionItem" 59 } 60 }] 61} 62 63# 質問を追加 64question_setting = form_service.forms().batchUpdate( 65 formId=FORM_ID, body=update).execute() 66 67# 結果を出力 68getresult = form_service.forms().get(formId=FORM_ID).execute() 69print("\033[32m" + "Done!" + "\033[0m") 70
試したこと・参考URL
Pythonでファイルに書き込まれたJSON文字列をパースする - Qiita
https://qiita.com/r-wakatsuki/items/105ef1b4ad843eb0e095
フォームやテストを更新する - Google Developers
https://developers.google.com/forms/api/guides/update-form-quiz?hl=ja
REST Resource: forms - Google Developers
https://developers.google.com/forms/api/reference/rest/v1/forms#ChoiceQuestion
補足情報(FW/ツールのバージョンなど)
- Visual Studio Code 1.74.3
- python 3.11(Microsoft Store)
- Google Forms API
あなたの回答
tips
プレビュー