Android Accountmanager身份验证令牌,不含clientID和clientSecret

Android Accountmanager身份验证令牌,不含clientID和clientSecret,android,accountmanager,auth-token,Android,Accountmanager,Auth Token,您好,我正在我的应用程序上检索具有以下代码的身份验证令牌: private String updateToken(boolean invalidateToken, int accountref) { String authToken = "null"; try { AccountManager am = AccountManager.get(TestAuthActivity.this); Account[] accounts = am.getAcc

您好,我正在我的应用程序上检索具有以下代码的身份验证令牌:

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;
}
我得到的令牌使用相同的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());
    }

}
publicstaticvoidcreatesession(上下文con,字符串authToken){
字符串结果=null;
InputStream=null;
ArrayList nameValuePairs=新的ArrayList();
添加(新的BasicNameValuePair(“authToken”,authToken));
试一试{
HttpClient HttpClient=新的DefaultHttpClient();
HttpPost HttpPost=新的HttpPost(“http://192.168.1.13/loginSession/authActivity4.php");
setEntity(新的UrlEncodedFormEntity(nameValuePairs));
HttpResponse response=httpclient.execute(httppost);
HttpEntity=response.getEntity();
is=entity.getContent();
}捕获(例外e){
Log.i(“taghttppost”,“e.toString());
}
//卡拉克特连锁店的转换
试一试{
BufferedReader reader=新的BufferedReader(新的InputStreamReader(
是“UTF-8”);
StringBuilder sb=新的StringBuilder();
字符串行=null;
而((line=reader.readLine())!=null){
sb.追加(第+行“\n”);
}
is.close();
结果=sb.toString();
}捕获(例外e){
Log.i(“tagconvertstr”,“e.toString());
}
//唐尼疗养院
试一试{
Log.i(“tagconvertstr”、“[”+结果+“]”);
JSONObject jObj=新JSONObject(结果);
long userID=jObj.getLong(“用户id”);
对话框d=新对话框(con);
d、 setTitle(String.valueOf(userID));
d、 show();
}捕获(JSONException e){
Log.i(“tagjsonexp”,“e.toString());
}捕获(解析异常){
Log.i(“tagjsonpars”,“e.toString());
}
}

此API不接受clientID或clientSecret作为输入。AccountManager将使用您保存的Google凭据在后台调用任何需要的Web API来获取令牌,或者返回缓存令牌(如果可用)。如果它返回了一个没有错误的令牌,那么它应该是有效的。试试看

如果您使用的是令牌类型“
oauth2:https://www.googleapis.com/auth/userinfo.email
“正如您在稍后的更新中提到的

我有一个代码非常相似的测试应用程序,当使用令牌类型“
oauth2:https://www.googleapis.com/auth/userinfo.email
“我得到了一个有效的令牌(在我的手机上运行Android 2.3.3),所以它应该可以工作

为了确保令牌有效,我记录令牌,从日志中复制它并加载
https://accounts.google.com/o/oauth2/tokeninfo?access_token=
在Chrome中。当我这样做时,我得到了预期的响应,没有任何错误

也许你可以测试同样的方法来缩小问题的范围

更新:

这只会在您获得的初始令牌有效时起作用,在它过期后,您尝试使用它时将收到一个错误。令牌过期后,我的测试应用程序在一段时间后停止工作。当我将其更改为在收到令牌后始终使其无效时,它再次开始工作

出于某种原因调用
am.invalidateAuthToken(“com.google”,null)oauth2:https://www.googleapis.com/auth/userinfo.email
”,因此您必须在要使令牌无效时指定该令牌(就像您的代码所做的那样)


因此,如果您确保始终在invalidateToken参数设置为true的情况下调用updateToken()方法,这应该适用于您。

是的,但当我尝试从服务器获取令牌信息时,它会向我发送“invalid token”==>检查我的消息我添加了服务器代码这不是它的工作方式。您似乎正在为contacts API获取一个令牌,而该令牌自然仅适用于contacts API。如果获得“”的令牌,则可以对其进行验证,然后获取用户信息。请参阅此处了解更多信息:当我输入“googleapis.com/auth/userinfo.email”或“scope”时,我甚至无法获得令牌,因为它告诉我“您输入了错误的密码或您的帐户已更改。请重新输入您的密码。”这是一个带有编辑文本字段的对话框,我必须在其中输入密码,当我输入密码时,消息会一次又一次地出现。。。我尝试使用另一个帐户,结果是一样的:顺便说一句,oauthssodemo.appspot.com并没有真正适应android和php脚本,所以我不完全理解:android的AccountManager的toke类型是IIRC,“oauth2:”,它确实可以工作,但可能取决于设备的android版本。演示中没有什么特别的东西,只有get和post,所以在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());
    }

}