撤销应用程序的帐户权限

发布于 2024-11-03 18:57:22 字数 180 浏览 4 评论 0原文

我编写了一个代码,使用 getAuthToken() 向客户经理请求 AuthToken。第一次 - 用户需要“允许”身份验证,但以后就不需要了。

我想知道是否有办法使用 android 系统或代码撤销该权限,以帮助我调试我的程序(我的帐户已经用完了:))。卸载该应用程序没有帮助。

谢谢你,

乌迪

I wrote a code that request an AuthToken from the account manager, using the getAuthToken(). On the first time - the user needs to "Allow" the authentication, but later on there's no need to.

I want to know if there's a way to revoke that permission, using the android system or code, in order to help me debug my program (I'm running out of accounts :)). Uninstalling the app doesn't help.

Thank you,

Udi

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

摇划花蜜的午后 2024-11-10 18:57:22

我发现当您删除并重新添加帐户时,权限会被撤销,您必须再次允许它。

这是我发现的最简单的方法,除非我能找到更好的方法,否则我会将其标记为答案。

I've found that when you remove and re-add the account, then the permission is revoked, and you have to allow it again.

That's the easiest way i've found, I'm marking this as the answer unless I'll get a better one.

ぇ气 2024-11-10 18:57:22

您可能需要完全卸载/重新安装才能有效撤销它。此外,如果您使用特定的sharedUserId,则可以在卸载后更改sharedUserId,使其看起来像不同的帐户。最后,您可以使用不同的证书对其进行签名。这就是我能够摆脱的,但是一个干净的 API 来撤销(甚至只是一个 Activity)会很好。

You might need to do a full uninstall/reinstall to in effect revoke it. Also, if you are using a specific sharedUserId, you can change the sharedUserId after you uninstall so it looks like a different account. Finally, you can sign it with a different cert. That's what I've been able to get away with, but a clean API to revoke (or even just an Activity) would be nice.

少女净妖师 2024-11-10 18:57:22

我尝试使用反射(仅用于测试目的)。
不幸的是,它抛出一个 SecurityException,因为 Android 检查调用者是一个系统应用程序...

作为参考,下面是代码:

/**
     * Revoke the fact that current app is allowed to retrieve an authToken for an account.
     * @param accountName The account whose permissions are being revoked
     * @param context current context
     * @return true if revoked successfully, false otherwise
     */
    public static boolean revokeAppPermission(String accountName, Context context) {
        if (accountName == null) {
            Log.w(TAG, "revokeAppPermission: abort, account missing.");
            return false;
        }

        AccountManager accountManager = AccountManager.get(context);
        Account[] accounts = accountManager.getAccounts();
        Account accountToRevoke = null;
        for (Account account : accounts) {
            if (accountName.equals(account.name)) {
                accountToRevoke = account;
                break;
            }
        }

        if (accountToRevoke == null) {
            Log.w(TAG, "revokeAppPermission: abort, no account found.");
            return false;
        }

        try {
            // public void updateAppPermission(Account account, String authTokenType, int uid, boolean value) {
            Method updateAppPermissionMethod = AccountManager.class.getDeclaredMethod("updateAppPermission", 
                    Account.class, String.class, int.class, boolean.class);
            updateAppPermissionMethod.setAccessible(true);
            updateAppPermissionMethod.invoke(accountManager, // Instance to invoke the method on 
                    accountToRevoke, // account 
                    "oauth2:https://www.googleapis.com/auth/somegoogleservice", // authTokenType
                    context.getApplicationInfo().uid, // uid
                    false); // false to revoke
        } catch (Exception e) {
            Log.w(TAG, "revokeAppPermission: Failed:" + e.getMessage());
            e.printStackTrace();
            return false;
        }

        return true;
    }

I tried using reflexion (for testing purposes only).
Unfortunately, it throws a SecurityException because Android checks that the caller is a System app...

For reference, here is the code:

/**
     * Revoke the fact that current app is allowed to retrieve an authToken for an account.
     * @param accountName The account whose permissions are being revoked
     * @param context current context
     * @return true if revoked successfully, false otherwise
     */
    public static boolean revokeAppPermission(String accountName, Context context) {
        if (accountName == null) {
            Log.w(TAG, "revokeAppPermission: abort, account missing.");
            return false;
        }

        AccountManager accountManager = AccountManager.get(context);
        Account[] accounts = accountManager.getAccounts();
        Account accountToRevoke = null;
        for (Account account : accounts) {
            if (accountName.equals(account.name)) {
                accountToRevoke = account;
                break;
            }
        }

        if (accountToRevoke == null) {
            Log.w(TAG, "revokeAppPermission: abort, no account found.");
            return false;
        }

        try {
            // public void updateAppPermission(Account account, String authTokenType, int uid, boolean value) {
            Method updateAppPermissionMethod = AccountManager.class.getDeclaredMethod("updateAppPermission", 
                    Account.class, String.class, int.class, boolean.class);
            updateAppPermissionMethod.setAccessible(true);
            updateAppPermissionMethod.invoke(accountManager, // Instance to invoke the method on 
                    accountToRevoke, // account 
                    "oauth2:https://www.googleapis.com/auth/somegoogleservice", // authTokenType
                    context.getApplicationInfo().uid, // uid
                    false); // false to revoke
        } catch (Exception e) {
            Log.w(TAG, "revokeAppPermission: Failed:" + e.getMessage());
            e.printStackTrace();
            return false;
        }

        return true;
    }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文