简体   繁体   中英

Google calendar access via java

I have a home integrated project working with google calendar...well, it was working. I've been using it for at least 6 months, maybe a year, I forget. Suddenly google changed the rules, and I can't figure out how to make things work now.

I don't want to use a whole library to do the extremely basic operations I need to do. I don't need a bunch of extra libraries in my Tomcat app.

Here is the full code sample that used to post a new calendar event, and get the id back so that we could later delete it if we wanted to for an update, etc.

I only get 403 errors back now, and the user/pass is OK, I can get my auth token, I can also login with a browser, I did the captcha unlock page, etc. It just stopped working on 11/18/2014. It was working on 11/17/2014.

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

Help? urlc.getInputStream() throws the exception.

I would be happy to use OAuth2 as well, but I can't get over the aspect that all the docs indicate to use a library, and that the user is going to be presented with the google page to accept. They can't be...they don't interact with this. This is an automated server side app building out calendar events. There is no user present or web browser. So I don't get what to do...they have the service account item, and I downloaded my private key, but I see nowhere that they tell you what you are supposed to do with the private key...

I'm happy to do CalDAV too, but again, OAuth keeps me from proceeding. I have no issues with the technical aspects after login, but I can't understand google's login architecture to get that far anymore.

--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);

So I am partially answering my own question...

The "solution" is having a refresh token. This can be used offline to get new access tokens on demand that are good for about 1 hour. You submit your refresh token to: ht tps :/ /account s. go ogle .c om/o/oauth2/token and it will give you back a "Bearer" access token to use for the next hour.

To get your refresh token though, you need to go to a URL in your browser to get the access, and your allowed redirect URLs must be configured to where you are going to 'redirect' to. It can be something invalid, so long as you can get the 'code' parameter its going to give you. You will need this code to then get the refresh token.

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

An example URL to go to is 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_developer_console&response_type=code&client_id=some_google_randomized_id.apps.googleusercontent.com&access_type=offline&approval_prompt=force

All of this info was pulled from:

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

So with all of this, you can now do the normal calendar API calls directly and pass in the Bearer authorization header.

So in total, you need exactly 0 google libraries to do all of this, they just make it very difficult to get to the meat of what is really going on. Half the "examples" even on google's pages are referencing invalid things. Most spend the majority of the example telling you how to reconfigure your eclipse to do the example...

The other side effect is this also requires the json format for calendar entries, and not the former XML style gcal was using. Not really a downside, just a change.

Until next year when it all breaks again...

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

Where calid should be replaced by the "calendar ID" of the calendar to be accessed. This can be found through the Google Calendar web interface as follows: in the pull-down menu next to the calendar name, select Calendar Settings. On the resulting page the calendar ID is shown in a section labelled Calendar Address. The calendar ID for a user's primary calendar is the same as that user's email address.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM