谷歌棄用的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_id
,client_secret
和redirect_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
但是,如果用戶已經做出了這樣的授權?如果我只有accessToken(+ appToken + appSecret)會怎麼樣? v3有可能嗎?你的getCalendars方法如何實現? – Karussell
啊,它看起來應該是這樣的: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
是的,這工作+我必須啓用日曆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