Android SecurityException: caller uid XXXX 与验证者的 uid 不同

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3774282/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 12:05:43  来源:igfitidea点击:

SecurityException: caller uid XXXX is different than the authenticator's uid

android

提问by Paul

I received the above exception when trying to implement Sample Sync Adapter application. I have seen numerous posts related to this issue but no satisfactory response.

我在尝试实现示例同步适配器应用程序时收到上述异常。我看过很多与这个问题相关的帖子,但没有令人满意的回应。

So I will jot down my solutionhere in case anyone else gets into the same issue.

所以我会在这里记下我的解决方案,以防其他人遇到同样的问题。

采纳答案by Paul

First, check the condition explained on this post:

首先,检查这篇文章中解释的条件:

[...] If you see an error from the AccountManagerServiceof the form caller uid XXXX is different than the authenticator's uid, it might be a bit misleading. The ‘authenticator' in that message is not your authenticator class, it's what Android understands to be the registered authenticator for the account's type. The check that happens within the AccountManagerServicelooks like this:

[...] 如果您看到AccountManagerService表单中的错误caller uid XXXX is different than the authenticator's uid,可能有点误导。该消息中的“身份验证器”不是您的身份验证器类,Android 将其理解为帐户类型的注册身份验证器。内发生的检查AccountManagerService如下所示:

 private void checkCallingUidAgainstAuthenticator(Account account) {
     final int uid = Binder.getCallingUid();
     if (account == null || !hasAuthenticatorUid(account.type, uid)) {
         String msg = "caller uid " + uid + " is different than the authenticator's uid";
         Log.w(TAG, msg);
         throw new SecurityException(msg);
     }
     if (Log.isLoggable(TAG, Log.VERBOSE)) {
         Log.v(TAG, "caller uid " + uid + " is the same as the authenticator's uid");
     }
 }

Note that hasAuthenticatorUid()takes the account.type. This is where I'd screwed up. I was creating my Accountwith a type specified by a constant:

请注意,hasAuthenticatorUid()account.type. 这是我搞砸的地方。我正在Account使用由常量指定的类型创建我的:

 class LoginTask {
     Account account = new Account(userId, AuthenticatorService.ACCOUNT_TYPE);
     ...
 }

 class AuthenticatorService extends Service {
     public static final String ACCOUNT_TYPE = "com.joelapenna.foursquared";
     ...
 }

but this constant did not match the XML definition for my authenticator:

但是这个常量与我的身份验证器的 XML 定义不匹配:

 <account-authenticator xmlns:android="/web/20150729061818/http://schemas.android.com/apk/res/android"
        android:accountType="com.joelapenna.foursquared.account" ... />

Second, if you are like me and want to embed the sample into your existing app for testing then, make sure you use Constantsclass that is part of this example and not under android.provider.SyncStateContractpackage. Because both classes use the same attribute name ACCOUNT_TYPEthat is used when creating Accountobject.

其次,如果您像我一样想要将示例嵌入到您现有的应用程序中进行测试,那么请确保您使用Constants本示例中的类而不是android.provider.SyncStateContractpackage.json 中的类。因为这两个类使用ACCOUNT_TYPE创建Account对象时使用的相同属性名称。

回答by Jan Berkel

Some other useful tips to debug problems like this.

调试此类问题的其他一些有用技巧。

First enable verbose logging for some tags:

首先为某些标签启用详细日志记录:

$ adb shell setprop log.tag.AccountManagerService VERBOSE
$ adb shell setprop log.tag.Accounts VERBOSE
$ adb shell setprop log.tag.Account VERBOSE
$ adb shell setprop log.tag.PackageManager VERBOSE

You'll see logging like this:

你会看到这样的日志:

V/AccountManagerService: initiating bind to authenticator type com.example.account
V/Accounts: there is no service connection for com.example.account
V/Accounts: there is no authenticator for com.example.account, bailing out
D/AccountManagerService: bind attempt failed for Session: expectLaunch true, connected false, stats (0/0/0), lifetime 0.002, addAccount, accountType com.example.account, requiredFeatures null

Which means that there is no authenticator registered for this account type. To see which authenticators are registered watch the log when installing the package:

这意味着没有为此帐户类型注册的身份验证器。要查看注册了哪些身份验证器,请在安装软件包时查看日志:

D/PackageManager: encountered new type: ServiceInfo: AuthenticatorDescription {type=com.example.account}, ComponentInfo{com.example/com.example.android.AuthenticatorService}, uid 10028
D/PackageManager: notifyListener: AuthenticatorDescription {type=com.example.account} is added

I had the problem that the authenticator xml descriptor referred to a string resource which didn't get resolved properly during the installation:

我遇到的问题是身份验证器 xml 描述符引用了一个在安装过程中没有得到正确解析的字符串资源:

android:accountType="@string/account_type"

The logs showed

日志显示

encountered new type: ServiceInfo: AuthenticatorDescription {type=@2131231194}, ...

Replacing it with a normal string (not resource) solved the problem. This seems to be Android 2.1 specific.

用普通字符串(不是资源)替换它解决了这个问题。这似乎是特定于 Android 2.1 的。

android:accountType="com.example.account"

回答by Farrukh Najmi

In my case the problem was very simply a mismatch in accountType declared in res/xml/authenticator.xmlas android:accountType="com.foo"but referenced incorrectly as "foo.com"in creating the Account:

在我的情况下,问题很简单,就是在res/xml/authenticator.xmlas中声明的 accountType 不匹配,android:accountType="com.foo""foo.com"在创建帐户时引用不正确:

Account newAccount = new Account("dummyaccount", "foo.com");

Doh!

哦!

回答by DocFoster

There are few parts to implement custom account...

实现自定义帐户的部分很少......

To invoke AccountManager in your Activity, something like that you already implemented...

要在您的活动中调用 AccountManager,您已经实现了类似的东西......

Account account = new Account(username, ACCESS_TYPE);
AccountManager am = AccountManager.get(this);
Bundle userdata = new Bundle();
userdata.putString("SERVER", "extra");

if (am.addAccountExplicitly(account, password, userdata)) {
    Bundle result = new Bundle();
    result.putString(AccountManager.KEY_ACCOUNT_NAME, username);
    result.putString(AccountManager.KEY_ACCOUNT_TYPE, ACCESS_TYPE);
    setAccountAuthenticatorResult(result);
}

In res/xml/authenticator.xml you have to define your AccountAuthenticator data (responsible for your Authenticator UID). ACCESS_TYPE have to be the same string as your defined accountType in this xml!

在 res/xml/authenticator.xml 中,您必须定义您的 AccountAuthenticator 数据(负责您的 Authenticator UID)。ACCESS_TYPE 必须与您在此 xml 中定义的 accountType 字符串相同!

<account-authenticator xmlns:android="http://schemas.android.com/apk/res/android"
    android:accountType="de.buecherkiste"
    android:icon="@drawable/buecher"
    android:label="@string/app_name"
    android:smallIcon="@drawable/buecher" >
</account-authenticator>

Finally you have to define your service your Manifest. Please do not forget the relevant permissions for manage your accounts (AUTHENTICATE_ACCOUNTS / USE_CREDENTIALS / GET_ACCOUNTS / MANAGE_ACCOUNTS)

最后,您必须将您的服务定义为 Manifest。请不要忘记管理您帐户的相关权限(AUTHENTICATE_ACCOUNTS / USE_CREDENTIALS / GET_ACCOUNTS / MANAGE_ACCOUNTS)

<service android:name=".AuthenticationService">
    <intent-filter>
        <action android:name="android.accounts.AccountAuthenticator" />
    </intent-filter>
    <meta-data android:name="android.accounts.AccountAuthenticator"
        android:resource="@xml/authenticator" />
</service>

回答by PLA

My error was assuming the AccountManager getAccounts() method returned accounts only associated with my application context. I changed from

我的错误是假设 AccountManager getAccounts() 方法返回仅与我的应用程序上下文关联的帐户。我从

AccountManager accountManager = AccountManager.get(context);
Account[] accounts = accountManager.getAccounts();

to

AccountManager accountManager = AccountManager.get(context);
Account[] accounts = accountManager.getAccountsByType(Constants.ACCOUNT_TYPE);

回答by clearfix

The same error will appear if you put incorrect values in your intent-filters in your manifest. I went through the android-dev tutorial on sync-adapters and ended up setting a bogus value for the "intent-filter/action android:name" as well as "meta-data/android:name" for syncadapter/accountauthenticator. This mistake caused the same errors to appear in the logs.

如果您在清单中的意图过滤器中放置了不正确的值,则会出现相同的错误。我浏览了关于同步适配器的 android-dev 教程,最终为“intent-filter/action android:name”以及同步适配器/accountauthenticator 的“meta-data/android:name”设置了一个虚假值。此错误导致日志中出现相同的错误。

For the record, the correct values are: {android.content.SyncAdapter, android.accounts.AccountAuthenticator}

为了记录,正确的值是:{android.content.SyncAdapter, android.accounts.AccountAuthenticator}

回答by Geoff

First off, take another look at Jan Berkel's excellent debugging advice.

首先,再看看 Jan Berkel 出色的调试建议。

Finally, another thing to check is that your content provider and the authentication, and sync services are declared as children of the applicationtag.

最后,要检查的另一件事是您的内容提供程序和身份验证以及同步服务是否被声明为application标签的子项。

    <application
        ...>
        <activity
            ...(Activity)...
        </activity>
        <provider
            ...(CP service declaration)/>

        <service
            ...(Authentication service declaration)...
        </service>

        <service
            ...(Sync service declaration)... 
        </service>
    </application>

回答by jrea

Make sure that your service XML is pointing to the correct location.

确保您的服务 XML 指向正确的位置。

For instance if you're module name is

例如,如果您的模块名称是

com.example.module.auth

com.example.module.auth

you're service android:name should be

你服务 android:name 应该是

<service android:name=".module.auth.name-of-authenticator-service-class"...

in AndriodManifest.xml

在 AndriodManifest.xml 中

回答by penduDev

For me it was a very silly mistake and was very hard to find.

对我来说,这是一个非常愚蠢的错误,很难找到。

In authenticator.xml I wrote

在 authenticationator.xml 我写

<account-authenticator xmlns:android="http://schemas.android.com/apk/res/android">
xmlns:android="http://schemas.android.com/apk/res/android"
android:accountType="com.myapp"
android:icon="@drawable/ic_launcher"
android:smallIcon="@drawable/ic_launcher"
android:label="@string/app_name"
/>

instead of

代替

<account-authenticator
xmlns:android="http://schemas.android.com/apk/res/android"
android:accountType="com.myapp"
android:icon="@drawable/ic_launcher"
android:smallIcon="@drawable/ic_launcher"
android:label="@string/app_name"
/>

which was causing this error. Hope this helps someone!

这导致了这个错误。希望这可以帮助某人!

回答by Ivan Vazhnov

In my case it was permissions in manifest file i had

就我而言,它是我拥有的清单文件中的权限

<uses-permission android:name="ANDROID.PERMISSION.GET_ACCOUNTS"/>

it was all caps, when i changed it to

都是大写的,当我把它改成

<uses-permission android:name="android.permission.GET_ACCOUNTS"/>

problem was gone

问题消失了