【问题标题】:Accountmanager Auth token whithout clientID and clientSecret没有 clientID 和 clientSecret 的 Accountmanager Auth 令牌
【发布时间】:2012-08-01 13:24:21
【问题描述】:

您好,我正在使用该代码在我的应用上检索身份验证令牌:

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());
    }

}

【问题讨论】:

    标签: android accountmanager auth-token


    【解决方案1】:

    鉴于您正在使用令牌类型“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=&lt;token&gt;。当我这样做时,我得到了预期的响应,没有任何错误。

    也许您可以进行相同的测试以缩小问题所在?

    更新:

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

    由于某种原因,当令牌类型为“oauth2:https://www.googleapis.com/auth/userinfo.email”时,调用am.invalidateAuthToken("com.google", null); 不会使令牌无效,因此当您想要使令牌无效时必须指定令牌(就像您的代码那样)。

    因此,如果您确保始终调用 updateToken() 方法并将 invalidateToken 参数设置为 true,这应该适合您。

    【讨论】:

      【解决方案2】:

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

      【讨论】:

      • 是的,但是当我尝试从我的服务器获取有关令牌的信息时,它会向我发送“无效令牌”==> 检查我的消息,我已经添加了我的服务器代码
      • 这不是它的工作原理。您似乎正在获取联系人 API 的令牌,并且该令牌自然仅适用于联系人 API。如果您获得“googleapis.com/auth/userinfo.email”的令牌,您可以对其进行验证,然后获取用户信息。请参阅此处了解更多信息:oauthssodemo.appspot.com
      • 当我输入“googleapis.com/auth/userinfo.email”或“googleapis.com/auth/userinfo.profile”时,我什至无法获得令牌,因为它告诉我“您输入了错误的密码或您的帐户已更改。请重新输入您的密码。”这是在一个带有edittext字段的对话框中,我必须在其中输入密码,当我输入密码时,消息会一次又一次地出现......我尝试使用另一个帐户,它是相同的:S顺便说一句oauthssodemo.appspot.com并没有真正适应 android 和 php 脚本所以我不明白这一切:S
      • Android 的 AccountManager 的令牌类型是 IIRC,'oauth2:googleapis.com/auth/userinfo.email',它确实有效,但可能取决于设备的 Android 版本。演示中没有任何具体内容,只有 GET 和 POST,因此它在 PHP 中的工作方式应该相同。
      • 如果你绝望了,你应该雇人帮忙。所以只能让你到目前为止。检查演示的代码,运行它并比较转储:也许你发送的东西格式错误,也许你缺少一个参数,可能是任何东西。这是:github.com/justinjsmith/oauthssodemo
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2017-09-01
      • 2014-09-03
      • 1970-01-01
      • 1970-01-01
      • 2019-11-05
      • 1970-01-01
      • 2013-06-03
      相关资源
      最近更新 更多