你好,我在我的代码的应用程序检索身份验证令牌:
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代码机器人但与这些2行改变:
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());
}
}