Communicating with the Google Calendar API using REST

Asked

Viewed 1,116 times

1

I’m using Scribe to authenticate with the API of Google Calendar. Authentication with Google is successfully done by returning the accessToken hassle-free. When I try to add one CalendarList, through the following code:

OAuthRequest request = new OAuthRequest(Verb.POST, "https://www.googleapis.com/calendar/v3/users/me/calendarList");
        JSONObject message = new JSONObject();
        message.put("id", "Teste 1");
        request.addPayload(message.toJSONString());
        service.signRequest(accessToken, request);
        Response response = request.send();

Google returns the following error this api does not support parsing form-encoded input

How should I build the JSON request to get a response from Google?

1 answer

2


public void createNewCalendarList(OAuthService service){
        Token newAccessToken = new Token( API_USER_TOKEN, API_USER_SECRET);
        OAuthRequest request = new OAuthRequest(Verb.POST, "https://www.googleapis.com/calendar/v3/calendars");
        JSONObject payload = new JSONObject();
        request.addHeader("Content-Type", "application/json ; charset=UTF-8");
        payload.put("summary", "Meeto");
        request.addPayload(payload.toJSONString());
        service.signRequest(newAccessToken, request);
        Response response = request.send();
        System.out.println("Reposta dos tipos da google: " + response.getBody());
    }

I had to add a Content-type, and a payload with all the necessary arguments. I used as a basis examples of the following link: https://developers.google.com/google-apps/calendar/v3/reference/calendars/insert

Browser other questions tagged

You are not signed in. Login or sign up in order to post.