使用java离线访问谷歌日历

我们有代码将我们的应用程序日历与登录用户的Google日历同步。 该代码使用的是AuthSub和CalendarService类,但它不提供使用访问令牌和刷新令牌的谷歌日历的离线访问,因为我想使用日历类来使用OAuth v3。 我面临的问题是将我的旧代码合并到没有getFeed()函数的新v3 Calendar类。 这是我的应用程序中的一些代码

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

请提供一些使用CalendarService进行离线访问的方法,这样我就不必更改代码了。 希望能快速回复。

谢谢 – 德拉维特古普塔

Google于2012年4月20日弃用了AuthSub。因此,您需要迁移到OAuth 2.0和Google Calendar API v3。 首先从以下链接下载jar文件:

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

从项目中删除旧日历和Authsub jar文件,并从此链接添加jar文件。

然后转到谷歌api控制台获取您的客户端ID,客户端密码并创建重定向uri。 并从相同的api控制台启用谷歌日历API。

我将为您提供一个示例代码,用于对用户进行身份validation,并显示他必须存储您在输出中获得的刷新令牌并存储它以便您可以脱机访问日历的日历。

以下function用于OAuth授权。

  public void authenticate(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException { String client_id = "xxxx"; String redirect_uri = "xxxxxx"; String scope = "https://www.googleapis.com/auth/calendar"; String client_secret = "xxxxxx"; List  scopes; HttpTransport transport = new NetHttpTransport(); JsonFactory jsonFactory = new JacksonFactory(); scopes = new LinkedList(); scopes.add(scope); GoogleAuthorizationCodeFlow flow = new GoogleAuthorizationCodeFlow.Builder(transport, jsonFactory, client_id, client_secret, scopes).build(); GoogleAuthorizationCodeRequestUrl url = flow.newAuthorizationUrl(); url.setRedirectUri(redirect_uri); url.setApprovalPrompt("force"); url.setAccessType("offline"); String authorize_url = url.build(); response.sendRedirect(authorize_url); } 

您必须将值添加到变量client_idclient_secretredirect_uri 。 所有这些值都在您的google api控制台中。

授权function将我转发到授权url,该url为我提供了访问令牌和刷新令牌。 但是,访问令牌在一段时间后过期。 因此,如果您需要访问令牌,则需要存储刷新令牌,并在您访问日历api时使用该令牌生成它。

以下函数生成访问令牌和刷新令牌,并在用户谷歌日历中打印日历列表。

 public void importCalendarList(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException { HttpSession session = request.getSession(); String staffKey = (String) session.getAttribute("staffKey"); ContactJdo staffDetails = staff.getStaffDetail(staffKey); String code = request.getParameter("code"); String calendarId=""; GoogleAuthorizationCodeFlow flow = new GoogleAuthorizationCodeFlow.Builder(transport, jsonFactory, client_id, client_secret, scopes).build(); GoogleTokenResponse res = flow.newTokenRequest(code).setRedirectUri(redirect_uri).execute(); String refreshToken = res.getRefreshToken(); String accessToken = res.getAccessToken(); List list1= getCalendars(accessToken); for(CalendarListEntry temp:list1) { System.out.println(temp.getId()); }} 

如果查看上面的函数,它会生成访问和刷新令牌。 如果要再次生成访问令牌,请使用以下函数:

 public static String getAccessToken(String refreshToken, String client_id, String client_secret) throws IOException { HttpTransport transport = new NetHttpTransport(); JsonFactory jsonFactory = new JacksonFactory(); GoogleRefreshTokenRequest req = new GoogleRefreshTokenRequest(transport, jsonFactory, refreshToken, client_id, client_secret); GoogleTokenResponse res = req.execute(); String accessToken = res.getAccessToken(); return accessToken; } 

将刷新令牌存储在某处,您可以执行日历文档中提到的所有操作。 在这里找到它

https://google-api-client-libraries.appspot.com/documentation/calendar/v3/java/latest/index.html

Interesting Posts