[Fixed] Google events list, problems with timeMin and timeMax

I’m trying to request my google api events list.

I have set everything up and when I send the request, I’m getting the complete list of events, leading me to conclude that I don’t have the right date format for timeMin and timeMax. However, when I use the exact same info in the api explorer, it works as expected…

When I run with q parameter, I get the expected results.
When I run with just the singleEvents and orderBy params (w/o timeMin and timeMax), I get the expected results.
When I run with the timeMin and timeMax params, as strings, that are exactly the same as what succeeds in the api explorier, I get 404 error:
error: {‘error’: {‘errors’: [{‘domain’: ‘global’, ‘reason’: ‘badRequest’, ‘message’: ‘Bad Request’}], ‘code’: 400, ‘message’: ‘Bad Request’}}

Code Sample:

def get_events(cal_id: str, date_start: datetime, days: int):
    print('calling get_events ...')
    user = anvil.users.get_user()
    url = f'https://www.googleapis.com/calendar/v3/calendars/primary/events'
    print(f"url: {url}")
    date_end = date_start + timedelta(days=1)
    print(date_start.isoformat())
    print(date_end.isoformat())
    refresh_token = anvil.secrets.decrypt_with_key('token_key', user['refresh_token'])
    access_token  = anvil.google.auth.refresh_access_token(refresh_token)
    try:
        response = anvil.http.request(url, json=True, method='GET',
                                      headers={'Authorization': 'Bearer ' + access_token},
                                      data={'singleEvents': True,  # running with just the top two params, gets expected results
                                            'orderBy': 'startTime',
                                      #       "timeMin": '2024-04-30T00:00:00-04:00',  ### this fails, giving 400 error, Bad Request
                                      #       "timeMax": '2024-05-01T00:00:00-04:00'
                                            }

                                      # data={ #this works as expected
                                      #     'q': 'Everett'
                                      # }
                                      
                                     )
    except anvil.http.HttpError as e:
        print(f"error: {e.content}")
        raise e
    if len(response['items']) < 250:
        print(response)
    print(len(response['items'])) # when the request succeeds, I get 250, the max
    print(response['items'][0]) # when the request succeeds, this is an appt from 2008

Bug Report
I would like this to be considered a bug report. The timeMin and timeMax parameters work as expected for the freeBusy search, but not for the events list. I think the anvil.http.request is not handling these two parameters correctly for the event list search.

Sometimes Google APIs result in 400s if you’re not using the right scopes.

What scopes are you using?
See docs:

When using Google’s api explorer all the scopes are turned on by default so it’s important to change the scopes to match what you actually use in your own app.
(See show scopes just above the execute button towards the bottom)

This is a bit of a gotcha with the API explorer.

Yes, this is a bug
For now you can put the parameters directly in the URL

from urllib.parse import urlencode

params = {
    "singleEvents": True,
    "orderBy": "startTime",
    "timeMax": "2024-04-01T00:00:00Z",
    "timeMin": "2024-01-01T00:00:00Z"
}

query_string = urlencode(params)
print(query_string)
url = url + "?" + query_string

Thanks for your help!

@bhaddaka - this should now be fixed

2 Likes

Thanks. I’ve been using your temporary fix and its been working fine. I just now tried the parameters in the anvil.http.request and it works fine for me too! Thanks again.

1 Like