繁体   English   中英

Accountmanager Auth令牌没有clientID和clientSecret

[英]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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM