[英]Accountmanager Auth token whithout clientID and 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);
,那個令牌有效嗎?
編輯5/08/2012:
這是我的新PHP腳本代碼,它試圖使用令牌獲取用戶的ID,但仍然從谷歌服務器獲得“無效令牌”:
<?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服務器:
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());
}
}
如果你在后面的更新中提到你正在使用令牌類型“ oauth2:https://www.googleapis.com/auth/userinfo.email
”,那么我的Android代碼會看到獲取令牌的錯誤。
我有一個代碼非常相似的測試應用程序,當使用令牌類型“ oauth2:https://www.googleapis.com/auth/userinfo.email
”時,我得到一個有效的令牌(在我的手機上運行Android 2.3.3),所以它應該工作。
要確保令牌有效,請記錄令牌,從日志中復制令牌並在Chrome中加載https://accounts.google.com/o/oauth2/tokeninfo?access_token=<token>
。 當我這樣做時,我得到了預期的響應,沒有任何錯誤。
也許你可以測試同樣的問題來縮小問題所在的范圍?
更新:
這只有在您獲得的初始令牌有效時才有效,在它到期后,當您嘗試使用它時會出現錯誤。 我的測試應用程序在令牌過期一段時間后停止工作。 當我將其更改為始終使令牌無效后,我再次開始工作。
出於某種原因,調用am.invalidateAuthToken("com.google", null);
當令牌類型為“ oauth2:https://www.googleapis.com/auth/userinfo.email
”時,不會使令牌無效,因此您必須在要使其無效時指定令牌(如您的代碼所示)。
因此,如果您確保始終使用invalidateToken參數設置為true調用updateToken()方法,則此方法應該適合您。
此API不將clientID或clientSecret作為輸入。 AccountManager將使用您保存的Google憑據通過在后台調用任何所需的Web API來獲取令牌,或者返回緩存的令牌(如果可用)。 如果它返回一個沒有錯誤的標記,它應該是有效的。 試試看。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.