B4J Question Problem Adding Event to Google Calendar

kostas_skarl

New Member
Licensed User
SOLVED. See next post for code.


I have succesfully added a "quick event", but I am having difficulties when I try to add an "event" to Google Calendar, as I am getting the 400 error message: "Missing end time."

I suspect that maybe the nested objects "start" and "end" are not correctly sent with the request.

I have also tried using a map object and JSONGenerator, instead of putting my values directly to the HTTP Request, but I am getting the same error.

Could someone please explain how to create this request?

Thanks in advance for your help.


Please find my code below:

B4X:
Sub add_event
   
    oauth2.GetAccessToken
    Wait For OAuth2_AccessTokenAvailable (Success As Boolean, Token As String)
    If Success = False Then
        Log("Error accessing account.")
        Return
    End If
   
    Dim Summary, StartDateTime, EndDateTime As String
    Dim BaseURL As String = "https://www.googleapis.com/calendar/v3/calendars/primary/events"
   
    Summary="My Summary"
    StartDateTime = "2019-04-19T12:00:00-07:00"
    EndDateTime = "2019-04-19T13:00:00-07:00"

    Dim j As HttpJob
    j.Initialize("", Me)
    j.PostString($"${BaseURL}/?access_token=${Token}"$ & " &summary='" & Summary & "'&start.dateTime='" & StartDateTime & "'&end.dateTime='" & EndDateTime & "'", "")
    j.GetRequest.SetContentType("application/json")
    Wait For (j) JobDone(j As HttpJob)
    If j.Success Then
        Log(j.GetString)

    Else
        Log(j.ErrorMessage)
        Log("Online data not available.")
    End If
    j.Release
End Sub
 
Last edited:

kostas_skarl

New Member
Licensed User
Sorry, problem solved...

I tried the json format again more carefully.

B4X:
Sub add_event
    
    oauth2.GetAccessToken
    Wait For OAuth2_AccessTokenAvailable (Success As Boolean, Token As String)
    If Success = False Then
        Log("Error accessing account.")
        Return
    End If
    
    Dim Summary, StartDateTime, EndDateTime As String
    Dim BaseURL As String = "https://www.googleapis.com/calendar/v3/calendars/primary/events"
    
    

    
    Summary="My Summary"
    StartDateTime = "2019-04-18T16:00:00-00:00"    'Europe/Athens
    EndDateTime = "2019-04-18T17:00:00-00:00"    'Europe/Athens
    
    Dim m As Map
    m.Initialize
    m.Put("summary":Summary)
    m.Put("start": CreateMap("dateTime":StartDateTime))
    m.Put("end": CreateMap("dateTime":EndDateTime))

    Dim jgen As JSONGenerator
    jgen.Initialize(m)


    Dim j As HttpJob
    j.Initialize("", Me)
    'j.PostString($"${BaseURL}/?access_token=${Token}"$ & " &summary='" & Summary & "'&start.dateTime='" & StartDateTime & "'&end.dateTime='" & EndDateTime & "'", "")
    j.PostString($"${BaseURL}/?access_token=${Token}"$,jgen.ToString )
    j.GetRequest.SetHeader ("Accept", "application/json")
    j.GetRequest.SetContentType("application/json")
    Wait For (j) JobDone(j As HttpJob)
    If j.Success Then
        Log(j.GetString)

    Else
        Log(j.ErrorMessage)
        Log("Online data not available.")
    End If
    j.Release
End Sub
 
Upvote 0

ivanomonti

Expert
Licensed User
Longtime User
Hi, do you by chance have an example to post so that I can understand how you designed it?

Thanks 10000
 
Upvote 0

DonManfred

Expert
Licensed User
Longtime User
Upvote 0

ivanomonti

Expert
Licensed User
Longtime User
Upvote 0
Top