使用客户端登录访问Google任务时出现问题
我正在尝试为Android编写应用程序以访问Google任务。我决定使用ClientLogin授权方法。使用客户端登录访问Google任务时出现问题
我从第一次POST请求获取ClientLogin“Auth”标记。然后我尝试用GET请求检索用户的任务列表。我为此编写了以下代码:
String requestString = "https://www.googleapis.com/tasks/v1/users/@me/lists";
String resultString = "";
try {
URLConnection connection1 = null;
URL url = new URL(requestString);
connection1 = url.openConnection();
HttpURLConnection httpsConnection1 = (HttpURLConnection)connection1;
httpsConnection1.setRequestMethod("GET");
httpsConnection1.setRequestProperty("Authorization", "GoogleLogin auth="+authkeyString);
httpsConnection1.setDoInput(true);
httpsConnection1.connect();
int responseCode = httpsConnection1.getResponseCode();
System.out.println(responseCode);
if (responseCode == HttpsURLConnection.HTTP_OK) {
InputStream in = httpsConnection1.getInputStream();
InputStreamReader isr = new InputStreamReader(in, "UTF-8");
StringBuffer data = new StringBuffer();
int c;
while ((c = isr.read()) != -1){
data.append((char) c);
}
resultString = new String (data.toString());
}
else{
resultString = "Errror - connection problem";
}
}
httpsConnection1.disconnect();
}
catch (MalformedURLException e) {
resultString = "MalformedURLException1:" + e.getMessage();
}
catch (IOException e) {
resultString = "IOException1:" + e.getMessage();
}
这是“authkeyString” - 带有授权标记的字符串变量。
当运行下真正的Android设备应用我接收:“IOException异常:SSL握手失败:失败是SSL库,通常一个协议错误.....”
此外,我试图运行从简单的这个代码从桌面Java应用程序:
Exception in thread "main" java.lang.IllegalArgumentException: Illegal character(s) in message header value: GoogleLogin auth=DQ ..... UT
at sun.net.www.protocol.http.HttpURLConnection.checkMessageHeader(HttpURLConnection.java:428)
at sun.net.www.protocol.http.HttpURLConnection.isExternalMessageHeaderAllowed(HttpURLConnection.java:394)
at sun.net.www.protocol.http.HttpURLConnection.setRequestProperty(HttpURLConnection.java:2378)
at sun.net.www.protocol.https.HttpsURLConnectionImpl.setRequestProperty(HttpsURLConnectionImpl.java:296)
at Tasks.main(Tasks.java:81)
ClientLogin用户使用用户名/密码
如果你想与Google APIs Client Library for Java使用ClientLogin,你需要建立一个Http支持认证的RequestFactory。
private static HttpTransport transport = new ApacheHttpTransport();
public static HttpRequestFactory createRequestFactory(
final HttpTransport transport) {
return transport.createRequestFactory(new HttpRequestInitializer() {
public void initialize(HttpRequest request) {
GoogleHeaders headers = new GoogleHeaders();
headers.setApplicationName("MyApp/1.0");
request.headers=headers;
try {
authorizeTransport(request);
} catch (Exception e) {
e.printStackTrace();
}
}
});
注意authorizeTransport方法,基本上会授权请求。 authorizeTransport看起来像这样:
private void authorizeTransport(HttpRequest request) throws HttpResponseException, IOException {
// authenticate with ClientLogin
ClientLogin authenticator = new ClientLogin();
authenticator.authTokenType = Constants.AUTH_TOKEN_TYPE;
authenticator.username = Constants.USERNAME;
authenticator.password = Constants.PASSWORD;
authenticator.transport = transport;
try {
Response response = authenticator.authenticate();
request.headers.authorization=response.getAuthorizationHeaderValue();
} catch (HttpResponseException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
您基本上通过提供用户名/密码来设置ClientLogin身份验证方法。 authenticate方法将根据提供的值进行身份验证,并返回可添加到HTTP标头以提供ClientLogin身份验证的响应对象。
的Android的AccountManager
为了与Android的AccountManager集成(避免Android用户在输入自己的用户名/密码),您可以在这里找到http://code.google.com/p/google-api-java-client/wiki/AndroidAccountManager
一些示例代码的事实用户不需要输入他的用户名/密码就可以增加用户的舒适度,但是解决方案仍然相对不安全。
我强烈建议执行以下操作:
使用客户端库
我会建议迁移到Google APIs Client Library for Java这种类型的交互。它是Android兼容的Java客户端库,适用于各种Google API。
您不希望因执行低级HTTP,安全性和JSON管道而烦恼。
Google Task API Developers guide也提到了相同的库。图书馆将负责您的身份验证。如果您想使用ClientLogin,您只需指定用户名/密码或与Android AccountManager集成即可。
避免使用ClientLogin的
的ClientLogin被认为是不安全的,并已发现有关该认证机制的一些安全漏洞。谷歌也doesn't recommend it。但是,如果您决定继续使用ClientLogin,Google API客户端库Java支持它。
你用什么serice参数来验证客户端登录方法中的任务? – 2012-09-05 14:37:18