2013-01-18 76 views
2

我們有代碼將我們的應用程序日曆與登錄用戶的Google日曆同步。該代碼使用AuthSub和CalendarService類,但它不提供使用訪問令牌和刷新令牌的谷歌日曆的離線訪問,因爲我想使用日曆類來使用OAuth v3。我面臨着將我的舊代碼合併到沒有getFeed()函數的新v3 Calendar類的問題。這裏是我的應用程序的一些代碼使用java離線訪問Google日曆

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這樣我就不必太大改變我的代碼給離線訪問。希望快速回復。

Thanks- Dravit古普塔

回答

3

谷歌棄用的AuthSub從2012年4月20日因此,它是你遷移到OAuth 2.0用戶和谷歌日曆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。

我給你一個樣本代碼,用於對用戶進行身份驗證並顯示他必須存儲輸出中獲取的刷新令牌並存儲它的日曆,以便可以脫機訪問日曆。

此功能用於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); 
} 

你要添加值的變量client_idclient_secretredirect_uri。所有這些值都在你的谷歌api控制檯中。

授權功能將我轉發給授權URL,授權URL給了我一個訪問令牌和一個刷新令牌。但是,訪問令牌在一段時間間隔後過期。因此,如果您需要訪問令牌,則需要存儲刷新令牌並使用該令牌在您訪問日曆API時生成它。

以下函數生成訪問令牌和刷新令牌,並在用戶的Google日曆中打印日曆列表。

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 <CalendarListEntry>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

+0

但是,如果用戶已經做出了這樣的授權?如果我只有accessToken(+ appToken + appSecret)會怎麼樣? v3有可能嗎?你的getCalendars方法如何實現? – Karussell

+0

啊,它看起來應該是這樣的:GoogleCredential憑證=新的GoogleCredential()。setAccessToken(「atoken」); client = new Calendar.Builder(new NetHttpTransport(),new JacksonFactory(),憑證) .setApplicationName(APPLICATION_NAME) .build(); - > https://groups.google.com/forum/#!topic/google-api-java-client/c33zpCkjJGk – Karussell

+0

是的,這工作+我必須啓用日曆API在谷歌控制檯+以下maven deps:' com.google.apis 谷歌的API服務,日曆 V3-rev47-1.15.0-RC com.google.api客戶端 谷歌的API - 客戶端 1 .15.0-RC com.google.http客戶端 谷歌-HTTP-客戶jackson2' – Karussell