值得一提的是,當應用程序與受保護數據的所有者之間存在高度信任時,可以使用ClientLogin。 通常建議在應用程序擁有受保護數據的情況下使用。
ClientLogin方法主要通過使用Google文檔中所述的特定參數將HTTP Post請求發送到Google服務來工作。 在本文中,我們將使用另一種方法來實現ClientLogin授權過程。 我們將使用Google APIs Java客戶端庫 ,這是一個功能強大的Java庫,用于訪問網絡上Google基于HTTP的API。 顯然,該庫中最重要的類是ClientLogin類。
ClientLogin類的1-解剖:
ClientLogin類提供單個方法authenticate(),該方法處理身份驗證過程的詳細信息。 它還提供了一個重要的內部類ErrorInfo,可用于處理身份驗證錯誤和驗證碼質詢邏輯。
在這篇文章中,我們為ClientLogin提供了一個干凈的包裝類,它處理完整的ClientLogin授權過程,包括身份驗證錯誤解析和驗證碼質詢處理。
2-google-api-java-client Maven依賴關系:
我們選擇使用maven構建我們的項目示例。 Maven為Java的Google API客戶端庫提供了相關性。 只需將以下maven依賴項添加到pom.xml文件中:
<dependency><groupId>com.google.api.client</groupId><artifactId>google-api-client-googleapis-auth-clientlogin</artifactId><version>1.2.3-alpha</version>
</dependency>
<dependency><groupId>com.google.api.client</groupId><artifactId>google-api-client-javanet</artifactId><version>1.2.3-alpha</version>
</dependency>
之后,使用maven:install安裝所需的jar,使其包含在我們的項目類路徑中。
3-GoogleClientLogin包裝器類:
我們的包裝器類顯然包含對ClientLogin的引用。 它提供了實現身份驗證過程重要功能的公共方法。
GoogleClientLogin具有一個構造函數,該構造函數接受一個String,該String表示您請求其授權的Google服務(例如,Google Calendar的“ cl”)。 構造函數如下所示:
/**
* @param service
*/
public GoogleClientLogin(String service) {super();this.service = service;authenticator = new ClientLogin();transport = GoogleTransport.create();authenticator.authTokenType = service;
}
主要方法是authenticate(username,password),它采用兩個參數表示用戶輸入的用戶名和密碼:
/**
* @param username
* @param password
* @throws ClientLoginException
*/
public void authenticate(String username, String password)throws ClientLoginException {try {// authenticate with ClientLoginauthenticator.username = username;authenticator.password = password;Response response = authenticator.authenticate();this.authToken = response.auth;} catch (HttpResponseException e) {parseError(e);} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();}}
此方法設置ClientLogin變量(用戶名和密碼),然后調用ClientLogin.authenticate(),該響應返回一個Response實例。 如果ClientLogin.authenticate()調用成功,我們將存儲身份驗證令牌'Response.auth'。 authenticate(username,password)包裝器方法的優點是對身份驗證錯誤的智能處理。
解析身份驗證錯誤:
我們區分在調用Clientlogin.authenticate()期間可能引發的兩個錯誤類別:
我們使用ClientLoginException類的一個不可恢復的錯誤
當Google服務需要驗證碼挑戰時引發的ba可恢復錯誤。
稍后,我們使用一個單獨的Exception類CaptchaRequiredException,它擴展了第一個ClientLoginException類。
如果身份驗證響應包含錯誤代碼,則Clientlogin.authenticate()會引發HttpResponseException。 我們提供了一個用于解析此異常類的輔助方法,如下所示:
/*** @param e* @throws ClientLoginException*/
private void parseError(HttpResponseException e)throws ClientLoginException {try {ClientLogin.ErrorInfo errorInfo = e.response.parseAs(ClientLogin.ErrorInfo.class);errorMessage = errorMsg.get(errorInfo.error);if (errorInfo.error.equals(CaptchaRequired)) {captchaToken = errorInfo.captchaToken;captchaUrl = errorInfo.captchaUrl;throw new CaptchaRequiredException(errorMessage, e);} elsethrow new ClientLoginException(errorMessage, e);} catch (IOException e1) {throw new ClientLoginException(e1);}
}
我們調用HttpResponseException.response.parseAs(ClientLogin.ErrorInfo.class)來解析響應。 如果錯誤代碼為“ CaptchaRequired”,則我們將存儲errorInfo.captchaToken和errorInfo.captchaUrl,然后拋出CaptchaRequiredException。 對于其余的錯誤代碼,我們只拋出ClientLoginException。
驗證碼驗證
對于驗證碼質詢,我們提供了第二個authenticate()方法,該方法提供一個額外的參數“ captchaAnswer”,表示用戶在驗證碼質詢期間輸入的驗證碼密鑰:
/*** @param username* @param password* @param captchaAnswer* @throws ClientLoginException*/
public void authenticate(String username, String password,String captchaAnswer) throws ClientLoginException {authenticator.username = username;authenticator.password = password;authenticator.captchaToken = this.captchaToken;authenticator.captchaAnswer = captchaAnswer;try {Response response = authenticator.authenticate();this.authToken = response.auth;} catch (HttpResponseException e) {parseError(e);} catch (IOException e) {throw new ClientLoginException(e);}
}
在調用authenticator.authenticate()之前,此方法設置兩個額外的字段authenticator.captchaToken和authenticator.captchaAnswer。 此方法的錯誤處理與主要的authenticate(username,password)方法相同。
最后,我們提供了一種檢索將顯示給用戶的驗證碼圖像的方法:
/*** @return the captchaImage*/
public BufferedImage getCaptchaImage() {BufferedImage image = null;try {URL url = new URL("https://www.google.com/accounts/"+ getCaptchaUrl());image = ImageIO.read(url);} catch (MalformedURLException e) {// TODO Auto-generated catch blocke.printStackTrace();return null;} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();return null;}return image;
}
您可以在此處查看完整的GoogleClientLogin類源文件。
4-測試GoogleClient包裝器類
GoogleClientLoginDialog是一個擺動對話框,其中提供了有關如何使用GoogleClientLogin包裝器類的示例。 它提供了強制Google服務發送驗證碼挑戰的功能。 我們使用一個線程來執行此測試,該線程一直發送隨機密碼,直到Google響應驗證碼為止:
private class ForceCaptchaRunnable implements Runnable{public void run() {Random r = new Random();boolean isCaptcha = false;while (!isCaptcha) {try {client.authenticate(textField.getText().trim(),passwordField.getText().trim()+ r.nextInt(100));showMessage("Auth Token: "+client.getAuthToken());} catch (CaptchaRequiredException e1) {isCaptcha = true;showCaptcha(true);} catch (ClientLoginException e1) {}}}}

您可以查看和下載此示例項目Google代碼項目的完整源代碼: google-apis-utils 。
參考: Othman博客上我們JCG合作伙伴 Othman El Moulat的Java版Google ClientLogin實用程序 。
相關文章 :
- YouTube Java API入門
- Google Guava庫必需品
- Java Code Geeks Andygene Web原型
- 使用Spring Security保護GWT應用程序
翻譯自: https://www.javacodegeeks.com/2011/09/google-clientlogin-utility-in-java.html