你好,我檢索身份驗證令牌上我的應用程序與代碼:的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());
}
}
是的,但是當我試圖讓從我的服務器令牌信息發送我「無效令牌」 ==>檢查我的消息,我已經添加了我的服務器代碼 – Karly 2012-08-03 16:48:46
這不是它的工作原理。您似乎獲得了聯繫人API的令牌,並且該令牌自然只適用於聯繫人API。如果您獲得了「https://www.googleapis.com/auth/userinfo.email」的令牌,則可以驗證該令牌並獲取用戶信息。請參閱此處瞭解更多信息:https://oauthssodemo.appspot。com – 2012-08-04 13:59:48
當我輸入'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