2011-06-02 88 views
0

我正在嘗試爲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) 
+0

你用什麼serice參數來驗證客戶端登錄方法中的任務? – 2012-09-05 14:37:18

回答

0

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支持它。

+0

謝謝!我試圖使用客戶端庫爲android編寫測試應用程序 - 它工作正常。但OAuth在我看來並不方便。我想用ClientLogin來開始這個庫。您是否知道一些示例或顯示ClientLogin與客戶端庫一起使用的示例?我在任務開發者指南中沒有找到任何有關此信息。 – kazanuser 2011-06-03 14:14:41

+0

查看更新的答覆 – ddewaele 2011-06-03 15:10:49

+0

無後顧之憂。如果答案證明有用,請接受答案 – ddewaele 2011-06-03 23:17:14