2012-08-03 65 views
3

你好,我檢索身份驗證令牌上我的應用程序與代碼:的AccountManager身份驗證令牌內部消除clientID的和clientSecret

private String updateToken(boolean invalidateToken, int accountref) { 
    String authToken = "null"; 
    try { 
     AccountManager am = AccountManager.get(TestAuthActivity.this); 
     Account[] accounts = am.getAccountsByType("com.google"); 
     AccountManagerFuture<Bundle> accountManagerFuture; 
     if(TestAuthActivity.this == null){//this is used when calling from an interval thread 
      accountManagerFuture = am.getAuthToken(accounts[accountref], SCOPE_CONTACTS_API, false, null, null); 
     } else { 
      accountManagerFuture = am.getAuthToken(accounts[accountref], SCOPE_CONTACTS_API, null, TestAuthActivity.this, null, null); 
     } 
     Bundle authTokenBundle = accountManagerFuture.getResult(); 
     authToken = authTokenBundle.getString(AccountManager.KEY_AUTHTOKEN).toString(); 
     if(invalidateToken) { 
      am.invalidateAuthToken("com.google", authToken); 
      authToken = updateToken(false, accountref); 
     } 
    } catch (Exception e) { 
     e.printStackTrace(); 
    } 

    Dialog d = new Dialog(TestAuthActivity.this); 
    d.setTitle("Token :" + authToken); 
    d.show(); 


    return authToken; 
} 

而且我確實收到包含authToken! (雖然我沒有把在clientID的和clientSecret) ==>accountManagerFuture = am.getAuthToken(accounts[accountref], SCOPE_CONTACTS_API, null, TestAuthActivity.this, NULL (HERE), null);,是令牌是否有效?

編輯2012年5月8日:

這裏是試圖使用令牌來獲取用戶的ID但是我的新的PHP腳本代碼從谷歌服務器的「無效令牌」:

<?php 

if(isset($_POST['authToken'])){   


//curl -H 'Authorization: GoogleLogin auth="Your_ClientLogin_token"' https://www.google.com//m8/feeds/contacts/default/full 


    $var = $_POST['authToken']; 
    $url = "https://accounts.google.com/o/oauth2/tokeninfo?access_token='".$var."' ";  
    //$url = "https://www.google.com/accounts/OAuthAuthorizeToken?oauth_token='"<?php echo rfc3986_decode($_POST['authToken'])"' "; 

    //<?php echo $oauth->rfc3986_decode($accrss_token['oauth_token']) 
    // Initialize session and set URL. 
     $ch = curl_init(); 
     curl_setopt($ch, CURLOPT_URL, $url); 

     // Set so curl_exec returns the result instead of outputting it. 
     curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); 
     curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); 
     curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false); 

     // Get the response and close the channel. 
     $response = curl_exec($ch); 
     curl_close($ch); 

     echo(json_encode($response)); 

} 
?> 

我米獲得令牌是相同的Java代碼的Android但是這兩條線改變:

 if(TestAuthActivity.this == null){//this is used when calling from an interval thread 
      accountManagerFuture = am.getAuthToken(accounts[accountref], "oauth2:https://www.googleapis.com/auth/userinfo.email", false, null, null); 
     } else { 
      accountManagerFuture = am.getAuthToken(accounts[accountref], "oauth2:https://www.googleapis.com/auth/userinfo.email", null, TestAuthActivity.this, null, null); 
     } 

這裏是我要送從我的Android應用程序的令牌我的PHP小號erver:

public static void createSession(Context con, String authToken) { 

    String result = null; 
    InputStream is = null; 

    ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(); 

    nameValuePairs.add(new BasicNameValuePair("authToken", authToken)); 

    try { 
     HttpClient httpclient = new DefaultHttpClient(); 
     HttpPost httppost = new HttpPost("http://192.168.1.13/loginSession/authActivity4.php"); 
     httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs)); 
     HttpResponse response = httpclient.execute(httppost); 

     HttpEntity entity = response.getEntity(); 
     is = entity.getContent(); 

    } catch (Exception e) { 
     Log.i("taghttppost", "" + e.toString()); 

    } 

    // conversion de la réponse en chaine de caractère 
    try { 
     BufferedReader reader = new BufferedReader(new InputStreamReader(
       is, "UTF-8")); 

     StringBuilder sb = new StringBuilder(); 

     String line = null; 

     while ((line = reader.readLine()) != null) { 
      sb.append(line + "\n"); 
     } 

     is.close(); 

     result = sb.toString(); 
    } catch (Exception e) { 
     Log.i("tagconvertstr", "" + e.toString()); 
    } 
    // recuperation des donnees json 
    try { 
     Log.i("tagconvertstr", "[" + result + "]"); 

     JSONObject jObj = new JSONObject(result); 

     long userID = jObj.getLong("user_id"); 

     Dialog d = new Dialog(con); 
     d.setTitle(String.valueOf(userID)); 
     d.show(); 

    } catch (JSONException e) { 
     Log.i("tagjsonexp", "" + e.toString()); 
    } catch (ParseException e) { 
     Log.i("tagjsonpars", "" + e.toString()); 
    } 

} 

回答

3

我看不出什麼毛病的Android代碼,獲取給定的,你在以後的更新中提到您正在使用的令牌類型「oauth2:https://www.googleapis.com/auth/userinfo.email」令牌。

我有非常相似的代碼測試應用程序,並使用令牌類型「oauth2:https://www.googleapis.com/auth/userinfo.email」當我得到一個有效的令牌(運行在手機上的Android 2.3.3),所以它應該工作。

爲了確保令牌有效我登錄令牌,它在Chrome中的日誌和負載https://accounts.google.com/o/oauth2/tokeninfo?access_token=<token>複製。當我這樣做時,我可以得到預期的響應而沒有任何錯誤。

也許你可以測試相同的縮小是哪裏的問題可能是什麼?

更新:

這隻會只要你得到了最初的令牌是有效的工作,它期滿後,您嘗試使用它,你會得到一個錯誤。過了一段時間,令牌過期後,我的測試應用停止工作。當我收到它後,將其更改爲始終使令牌無效時,它再次開始工作。

由於某些原因,如果令牌類型爲「oauth2:https://www.googleapis.com/auth/userinfo.email」,則調用am.invalidateAuthToken("com.google", null);時不會使令牌失效,因此當您要使其無效時(如代碼所示),您必須指定令牌。

所以,如果你確保你總是打電話給你updateToken()方法設置爲true這應該爲你工作的invalidateToken參數。

2

此API不採用clientID或clientSecret作爲輸入。 AccountManager將使用您保存的Google憑證通過在後臺調用任何所需的Web API來獲取令牌,或返回緩存令牌(如果可用)。如果它沒有錯誤地返回一個標記,它應該是有效的。試試看。

+0

是的,但是當我試圖讓從我的服務器令牌信息發送我「無效令牌」 ==>檢查我的消息,我已經添加了我的服務器代碼 – Karly 2012-08-03 16:48:46

+1

這不是它的工作原理。您似乎獲得了聯繫人API的令牌,並且該令牌自然只適用於聯繫人API。如果您獲得了「https://www.googleapis.com/auth/userinfo.email」的令牌,則可以驗證該令牌並獲取用戶信息。請參閱此處瞭解更多信息:https://oauthssodemo.appspot。com – 2012-08-04 13:59:48

+0

當我輸入'googleapis.com/auth/userinfo.email'或'https://www.googleapis.com/auth/userinfo.profile'作爲範圍時,我甚至無法獲取令牌,它告訴我「您輸入了密碼錯誤或您的帳戶已更改,請重新輸入您的密碼。「這是在一個edittext字段的對話框中,我必須輸入密碼,當我輸入密碼時,信息會一次又一次地出現......我用另一個帳戶嘗試過,結果是一樣的:S順便說一句oauthssodemo.appspot.com是不是真的適應android和php腳本,所以我不明白這一切:S – Karly 2012-08-05 00:17:05