We have code to sync our application calendar with google calendar of logged in user. The code is using AuthSub and CalendarService class but it does not provide offline access to google calendar using access token and refresh token for that i want to use OAuth v3 using calendar class. I am facing problem to merge my old code to new v3 Calendar class which is not having getFeed() function. Here is some code from my application
if(StringUtil.isValid(request.getQueryString())) {
onetimeUseToken = AuthSubUtil.getTokenFromReply(request.getQueryString());
}
if(StringUtil.isValid(onetimeUseToken)) {
String sessionToken = AuthSubUtil.exchangeForSessionToken(onetimeUseToken,null);
CalendarService calendarService = new CalendarService("myapp");
calendarService.setAuthSubToken(sessionToken, null);
session.setAttribute("calendarServicesession",calendarService);
userIDforCalendar = (String) session.getAttribute("calendar_user_no");
}
CalendarFeed myResultsFeed1 =service.getFeed(new URL("https://www.google.com/calendar/feeds/default/allcalendars/full"),CalendarFeed.class);
for (int i = 0; i < myResultsFeed1.getEntries().size(); i++) {
CalendarEntry entry = myResultsFeed1.getEntries().get(i);
.....
}
Please provide me some way to give offline access using CalendarService so that I don't have to change my code much. Hoping for a quick reply.
Thanks- Dravit Gupta
Google deprecated AuthSub from 20 April 2012. So it is time you migrated to OAuth 2.0 and Google Calendar API v3. First download the jar files from these following links:
https://google-api-client-libraries.appspot.com/download/library/calendar/v3/java
http://google-oauth-java-client.googlecode.com/files/google-oauth-java-client-1.13.1-beta.zip
Remove the old calendar and Authsub jar files from your project and add the jar files from this link.
Then go to google api console to get your client id, client secret and create a redirect uri. And from the same api console enable google calendar api.
I am giving you a sample code that authenticates the user and shows the calendars that he has you have to store the refresh token that you get in the output and store it so that you can access the calendar offline.
This following function is for OAuth authorization.
You have to add values to the variables
client_id,client_secretandredirect_uri. All these values are in your google api console.The authorization function forwards me to the authorize url which gives me an access token and a refresh token. However, access token expires after a time interval. So if you want the access token you need to store the refresh token and using that generate it whenever you access the calendar api.
This following function generates access token and refresh token and prints the list of calendars in the users google calendar.
If you look at the above function, it generates both access and refresh tokens. If you want to generate access token again use this function:
Store the refresh token somewhere and you can do all the operations that are mentioned in the calendar documentation. Find it here
https://google-api-client-libraries.appspot.com/documentation/calendar/v3/java/latest/index.html