I am trying to build a free/busy body request to Google Calendar API via Python 3.8 . However, when I try to insert a new item into the body request, I am getting a bad request and can't use it.
This code is working:
SUBJECTA = '[email protected]'
SUBJECTB = '[email protected]'
body = {
"timeMin": now,
"timeMax": nownext,
"timeZone": 'America/New_York',
"items": [{'id': SUBJECTA},{"id": SUBJECTB} ]
}
Good Body result:
{'timeMin': '2019-11-05T11:42:21.354803Z',
'timeMax': '2019-11-05T12:42:21.354823Z',
'timeZone': 'America/New_York',
'items': [{'id': '[email protected]'},
{'id': '[email protected]'}]}
However, While using this code:
items = "{'ID': '[email protected]'},{'ID': '[email protected]'},{'ID': '[email protected]'}"
body = {
"timeMin": now,
"timeMax": nownext,
"timeZone": 'America/New_York',
"items": items
}
The Body results contain additional quotes at the start and end position, failing the request:
{'timeMin': '2019-11-05T12:04:41.189784Z',
'timeMax': '2019-11-05T13:04:41.189804Z',
'timeZone': 'America/New_York',
'items': ["{'ID': [email protected]},{'ID':
[email protected]},{'ID':
[email protected]},{'ID':
[email protected]},{'ID':
[email protected]}"]}
What is the proper way to handle it and send the item list in an accurate way?
items
is given by the string of "{'ID': '[email protected]'},{'ID': '[email protected]'},{'ID': '[email protected]'}"
.[{'ID': '[email protected]'}, {'ID': '[email protected]'}, {'ID': '[email protected]'}]
.If my understanding is correct, how about this answer? Please think of this as just one of several answers.
import json # Added
items = "{'ID': '[email protected]'},{'ID': '[email protected]'},{'ID': '[email protected]'}"
items = json.loads(("[" + items + "]").replace("\'", "\"")) # Added
body = {
"timeMin": now,
"timeMax": nownext,
"timeZone": 'America/New_York',
"items": items
}
print(body)
If now
and nownext
are the values of "now"
and "nownext"
, respectively, the result is as follows.
{
"timeMin": "now",
"timeMax": "nownext",
"timeZone": "America/New_York",
"items": [
{
"ID": "[email protected]"
},
{
"ID": "[email protected]"
},
{
"ID": "[email protected]"
}
]
}
If you can retrieve the IDs as the string value, I recommend the following method as a sample script.
ids = ['[email protected]', '[email protected]', '[email protected]']
items = [{'ID': id} for id in ids]
If I misunderstood your question and this was not the result you want, I apologize.