Revoke account permission for an app

不想你离开。 提交于 2019-12-20 23:29:44

问题


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


回答1:


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.




回答2:


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.




回答3:


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


来源:https://stackoverflow.com/questions/5805657/revoke-account-permission-for-an-app

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!