3

こんにちは、そのコードを使用してアプリで認証トークンを取得しています:

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 日:

これは、トークンを使用してユーザーの ID を取得しようとしているが、Google サーバーから「無効なトークン」を取得しようとしている私の新しい PHP スクリプト コードです。

<?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 コードを使用していますが、次の 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());
    }

}
4

2 に答える 2

3

oauth2:https://www.googleapis.com/auth/userinfo.email後の更新で言及したように、トークン タイプ " " を使用している場合、トークンを取得する Android コードに問題はありません。

非常によく似たコードのテスト アプリケーションがあり、トークン タイプ " oauth2:https://www.googleapis.com/auth/userinfo.email" を使用すると、有効なトークン (携帯電話で Android 2.3.3 を実行) を取得できるので、動作するはずです。

トークンが有効であることを確認するために、トークンをログに記録し、ログからコピーしてhttps://accounts.google.com/o/oauth2/tokeninfo?access_token=<token>Chrome に読み込みます。これを行うと、エラーなしで期待される応答が得られます。

同じことをテストして、問題がどこにあるかを絞り込むことができますか?

アップデート:

これは、取得した最初のトークンが有効である場合にのみ機能します。有効期限が切れると、使用しようとするとエラーが発生します。しばらくしてトークンの有効期限が切れると、テスト アプリが動作しなくなりました。受信後にトークンを常に無効にするように変更すると、再び機能し始めました。

何らかの理由でam.invalidateAuthToken("com.google", null);、トークンの種類が " oauth2:https://www.googleapis.com/auth/userinfo.email" の場合、呼び出しによってトークンが無効にならないため、トークンを無効にする場合はトークンを指定する必要があります (コードのように)。

そのため、invalidateToken パラメータを true に設定して updateToken() メソッドを常に呼び出すようにすれば、これでうまくいくはずです。

于 2012-11-07T10:16:42.393 に答える
2

このAPIは、clientIDまたはclientSecretを入力として受け取りません。AccountManagerは、保存されたGoogleクレデンシャルを使用して、必要なWeb APIをバックグラウンドで呼び出してトークンを取得するか、キャッシュされているトークンを返します(利用可能な場合)。エラーなしでトークンを返した場合、それは有効であるはずです。やってみよう。

于 2012-08-03T15:31:07.313 に答える