Accessing google calendar via java

I have a home integrated project working with google calendar ... well it worked. I have been using it for at least 6 months, maybe a year, I forgot. All of a sudden Google changed the rules and I can't figure out how to get everything to work now.

I don't want to use a whole library to do the basic operations that I need. I don't need a bunch of additional libraries in my Tomcat application.

Here's an example of the complete code that was used to post a new calendar event and returned ID so that we can delete it later if we want to do an update, etc.

I only get 403 errors and the user / pass is ok, I can get my authentication token, I can also log into the browser, I made a captcha unlock page, etc. It just stopped working on 11/18/2014. This worked on 11/17/2014.

Error: java.io.IOException: Server responded with HTTP response code: 403 for URL: https://www.google.com/calendar/feeds/ myuser@gmail.com / private / full

Help? urlc.getInputStream () throws an exception.

I would be happy to use OAuth2, but I cannot figure out that all the docs point to using the library and that the user will be presented with a google page for acceptance. They can't be ... they don't interact with it. It is an automated server side application that generates calendar events. No user or web browser. So I don't understand what to do ... they have a service account item and I have uploaded my private key, but I don't see anywhere that they tell you what you should do with the private key ...

I'm happy to do CalDAV too, but again, OAuth won't let me continue. I have no problem with the technical aspects after login, but I cannot understand the google login architecture to get much more.

- Ben

                            HttpURLConnection urlc = (HttpURLConnection)new URL("https://www.google.com/calendar/feeds/myuser@gmail.com/private/full").openConnection();
                            urlc.setDoOutput(true);
                            urlc.setFollowRedirects(false);
                            urlc.setRequestMethod("POST");
                            urlc.setRequestProperty("Content-Type", "application/atom+xml");
                            urlc.setRequestProperty("Authorization", "GoogleLogin auth=" + authToken);

                            OutputStream out = urlc.getOutputStream();
                            out.write(b);
                            out.close();

                            int code = urlc.getResponseCode();

                            String location = "";
                            for (int x=0; x<10; x++)
                            {
                                    System.out.println(x+":"+urlc.getHeaderFieldKey(x)+":"+urlc.getHeaderField(x));
                                    if (urlc.getHeaderFieldKey(x) != null && urlc.getHeaderFieldKey(x).equalsIgnoreCase("Location")) location = urlc.getHeaderField(x);
                            }
                            String result = consumeResponse(urlc.getInputStream());
                            System.out.println(result);
                            urlc.disconnect();

                            urlc = (HttpURLConnection)new URL(location).openConnection();
                            urlc.setDoOutput(true);
                            urlc.setFollowRedirects(false);
                            urlc.setRequestMethod("POST");
                            urlc.setRequestProperty("Content-Type", "application/atom+xml");
                            urlc.setRequestProperty("Authorization", "GoogleLogin auth=" + authToken);
                            out = urlc.getOutputStream();
                            out.write(b);
                            out.close();

                            code = urlc.getResponseCode();
                            result = consumeResponse(urlc.getInputStream());
                            System.out.println("Raw result:"+result);
                            gcal_id = result.substring(result.indexOf("gCal:uid value='")+"gCal:uid value='".length());
                            gcal_id = gcal_id.substring(0,gcal_id.indexOf("@google.com"));
                            System.out.println("Calendar ID:"+gcal_id);

      

+3


source to share


2 answers


Therefore, I partially answer my own question ...

There is a refresh token in the "solution". This can be used offline to get new on-demand access tokens which are good for about 1 hour. You send refresh token: ht tps: // account s. go ogle.c om / o / oauth2 / token and it will return you a "Bearer" access token to use for the next hour.

To get the refresh token, you need to navigate to the url in your browser to access, and your allowed redirect urls must be configured so that you redirect. It might be invalid if you can get the "code" parameter it will give you. You will need this code to get the refresh token.

Configure allowed redirect URLs in the developer console. Find your own dev console link. I have no points to tell you, apparently.

An example URL to navigate looks something like this:

https://accounts.google.com/o/oauth2/auth?scope=https%3A%2F%2Fwww.googleapis.com%2Fauth%2Fcalendar&state=&redirect_uri=url_encoded_url_to_redirect_to_that_is_in_det_console&responseponse force



All this information was extracted from:

https://developers.google.com/accounts/docs/OAuth2WebServer#refresh

So with all of this, you can now directly make regular calendar API calls and pass the bearer authorization header.

All in all, you need exactly 0 google libraries to do all of this, they just have a really hard time getting to the meat of what's really going on. Half of the "examples" even on Google pages refer to inappropriate things. Most of them carry out most of the examples, telling you how to readjust your eclipse to make an example ...

Another side effect is also the use of json format for calendar entries, rather than using the old XML style gcal. Not really a flaw, just a change.

Until next year when it all breaks down again ...

+1


source


https://apidata.googleusercontent.com/caldav/v2/calid/user

If the calibration needs to be replaced by the "calendar ID" of the calendar that needs to be accessed. This can be found through the Google Calendar web interface, as shown below: From the drop-down menu next to the calendar name, select Calendar Settings. On the resulting page, the calendar ID appears in the section labeled "Calendar Address". The calendar ID for the user's primary calendar is the same as this user's email address.



Please refer to the following link: - https://developers.google.com/google-apps/calendar/caldav/v2/guide

0


source







All Articles