Java中的Google ClientLogin实用程序

Google API的身份验证和授权是当今需要与Google服务集成和信息交换的应用程序中的常见功能。 尽管大多数Google身份验证过程是针对Web应用程序量身定制的,但它也可用于桌面和已安装的应用程序。 对于桌面应用程序,Google建议使用称为ClientLogin的身份验证方法。

值得一提的是,当应用程序与受保护数据的所有者之间存在高度信任时,可以使用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 ClientLogin
		authenticator.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 block
		e.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);

		} else
			throw 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 block
		e.printStackTrace();
		return null;

	} catch (IOException e) {
		// TODO Auto-generated catch block
		e.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实用程序

相关文章 :


翻译自: https://www.javacodegeeks.com/2011/09/google-clientlogin-utility-in-java.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值