使用java离线访问google日历

使用java离线访问google日历,java,oauth,google-calendar-api,authsub,Java,Oauth,Google Calendar Api,Authsub,我们有代码将我们的应用程序日历与登录用户的谷歌日历同步。代码使用AuthSub和CalendarService类,但它不提供使用access token和refresh token对googlecalendar的脱机访问,我想使用calendar类使用OAuth v3。我面临着将旧代码合并到新v3 Calendar类的问题,该类没有getFeed()函数。下面是我的应用程序中的一些代码 if(StringUtil.isValid(request.getQueryString())) {

我们有代码将我们的应用程序日历与登录用户的谷歌日历同步。代码使用AuthSub和CalendarService类,但它不提供使用access token和refresh token对googlecalendar的脱机访问,我想使用calendar类使用OAuth v3。我面临着将旧代码合并到新v3 Calendar类的问题,该类没有getFeed()函数。下面是我的应用程序中的一些代码

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

}
if(StringUtil.isValid(request.getQueryString())){
onetimeUseToken=AuthSubUtil.getTokenFromReply(request.getQueryString());
}       
如果(StringUtil.isValid(onetimeUseToken)){
字符串sessionToken=AuthSubUtil.exchangeForSessionToken(onetimeUseToken,null);
CalendarService CalendarService=新的CalendarService(“myapp”);
calendarService.setAuthSubToken(sessionToken,null);
setAttribute(“calendarServicesession”,calendarService);
userIDforCalendar=(String)session.getAttribute(“日历用户号”);
}
CalendarFeed myResultsFeed1=service.getFeed(新URL(“https://www.google.com/calendar/feeds/default/allcalendars/full),CalendarFeed.class);
对于(int i=0;i
请提供一些使用CalendarService进行脱机访问的方法,这样我就不必对代码进行太多更改。 希望能尽快得到答复

谢谢-
德拉维特·古普塔(Dravit Gupta)

谷歌从2012年4月20日起否决了AuthSub。因此,现在是迁移到OAuth 2.0和Google日历API v3的时候了。首先从以下链接下载jar文件:

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

然后转到GoogleAPI控制台获取您的客户机id、客户机机密并创建重定向uri。 并从同一api控制台启用google日历api

我将给您一个示例代码,用于验证用户并显示他拥有的日历。您必须存储在输出中获得的刷新令牌,并将其存储,以便脱机访问日历

以下函数用于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 <String> scopes;
    HttpTransport transport         = new NetHttpTransport();
    JsonFactory jsonFactory         = new JacksonFactory();

    scopes = new LinkedList<String>();
    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);
}
如果您查看上述函数,它将生成访问和刷新令牌。如果要再次生成访问令牌,请使用此功能:

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;
}
将刷新令牌存储在某个位置,您就可以执行日历文档中提到的所有操作。在这里找到它


但是如果用户已经进行了这样的授权怎么办?如果我只有accessToken(+appToken+appSecret),该怎么办?v3有可能吗?您的getCalendars方法是如何实现的?啊,看起来应该是这样的:GoogleCredential=new GoogleCredential().setAccessToken(“atoken”);client=new Calendar.Builder(new NetHttpTransport(),new JacksonFactory(),credential).setApplicationName(APPLICATION_NAME).build();->是 啊这起作用了+我必须在谷歌控制台中启用日历API+以下maven deps:
com.google.API谷歌API服务日历v3-rev47-1.15.0-rc com.google.API-client谷歌API客户端1.15.0-rc com.google.http-clientgoogle-http-client-jackson2
getCalendars方法私有列表getCalendars(字符串accessToken)抛出IOException{Calendar calendarService=AppointmentUtilities.getCalendar(accessToken);Calendar.CalendarList.List=calendarService.CalendarList().List();List.setOauthToken(accessToken);return List.execute().getItems();}对于您的第一个问题,当我们使用脱机模式时,刷新令牌会派上用场,因为访问令牌将过期。如果希望再次访问,则需要使用刷新令牌生成访问令牌
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;
}