您好,我正在使用该代码在我的应用程序上检索身份验证令牌:
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 日:
这是我的新 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());
}
}