3

I have the following Account authentication classes

public class AppAuthenticator extends AbstractAccountAuthenticator {

private Context mContext;

public AppAuthenticator(Context context) {
    super(context);
    mContext = context;
}

//This is used by the login activity when the guy logs in after starting the app
public Bundle addAccount(AccountAuthenticatorResponse response, String accountType, String authTokenType, 
        String[] requiredFeatures, Bundle options)
        throws NetworkErrorException {
        //...Code Here...
    }

}

with

public class AuthenticationService extends Service {

private static AppAuthenticator sAuthenticator = null;

public AuthenticationService() {
    super();
}

@Override
public IBinder onBind(Intent intent) {
    IBinder res = null;
    if (intent.getAction().equals(android.accounts.AccountManager.ACTION_AUTHENTICATOR_INTENT))
        res = getAuthenticator().getIBinder();
    return res;
}

private AppAuthenticator getAuthenticator() {
    if (sAuthenticator == null)
        sAuthenticator = new AppAuthenticator(this);
    return sAuthenticator;
}
}

and the manifests: xml/authenticator.xml

<account-authenticator 
    xmlns:android="http://schems.android.com/apk/res/android"
    android:accountType="@string/account_type" 
    android:label="@string/app_name" 
    android:icon="@drawable/benchcap" />

androidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.app.app"
    android:versionCode="1"
    android:versionName="1.0" >

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

<application
    android:name="com.app.app.AppApplication"
    android:allowBackup="true"
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >

    <service android:name=".AuthenticationService"
        android:exported="true" android:process=":auth">
        <intent-filter>
            <action android:name="android.accounts.AccountAuthenticator" />
        </intent-filter>

        <meta-data
            android:name="android.accounts.AccountAuthenticator"
            android:resource="@xml/authenticator" />
    </service>
</application>

with @string/app_name="App" and @string/account_type="com.app.app"

So the problem is when the application is installed I get

"Unable to load service info ResolveInfo{40f00c40 com.app.app.AuthenticationService p=0 m=0x108000}
 Loaded meta-data for 0 account types, 0 accounts in 4ms(wall) 2ms(cpu)"

and I try to add accounts I get the SecurityException "Caller UID is different than the authenticator UID" (described here: SecurityException: caller uid XXXX is different than the authenticator's uid). Which makes sense since the service is not regiestered.

The system gives no more information than that.

What does one need to add to get the authentication service registered properly?

Community
  • 1
  • 1
muichkine
  • 2,890
  • 2
  • 26
  • 36
  • Have you tried using a regular string (not a resource) for the account type in the XML file? – Nikolay Elenkov Dec 19 '12 at 14:36
  • Your code seems correct, the only thing is that you are running the service in a separate process. Any particular reason for this? (the `android:process=":auth" bit) – Nikolay Elenkov Dec 19 '12 at 14:43
  • No, I picked the line from tutorials and I thought it was required. Let me try without it......and...I still get the "called uid error" at runtime. – muichkine Dec 19 '12 at 14:53
  • I don't know if this has to do, but I call the AppAutthenticator::addAccount() from an `ASyncTask` (different thread). – muichkine Dec 19 '12 at 14:55
  • Maybe it also has to do buth the AccountTypeManager says at deployment time: "No authenticator found for type=com.android.exchange, ignoring it." I have no clue where this type is defined and by who. – muichkine Dec 19 '12 at 15:03
  • It seems your authenticator is not properly picked up by the system. As for the UID error, you have to call the method from an app with the same UID, typically this means the same app. Different thread doesn't change the UID, so it shouldn't matter. BTW, 'com.android.exchange' is usually done by the stock Email app. – Nikolay Elenkov Dec 19 '12 at 16:02
  • Thanks for the info about the com.android.exchange thing. Now, why in the world would the system not pick the authenticator?!?! I've tried on 2 different virtual devices btw... – muichkine Dec 19 '12 at 16:08
  • It might be throwing an exception or something like that. Try catching and logging all exceptions and maybe change the account type. – Nikolay Elenkov Dec 19 '12 at 16:22
  • Tried changing the type. No result. Now, reading the full log, I can see no further error description than what I posted, nor exception. I'm turning nuts on this. :) – muichkine Dec 20 '12 at 15:44
  • You might be hitting some obscure bug. Does it work on an actual device? – Nikolay Elenkov Dec 20 '12 at 15:45
  • No idea man. Someone stole my phone 3 days ago. I'll keep you posted you when I get a new one somewhere in january... – muichkine Dec 20 '12 at 15:54
  • Here's another example for creating an Authenticator, maybe it'll help: http://udinic.wordpress.com/2013/04/24/write-your-own-android-authenticator/ – Udinic Apr 25 '13 at 15:12

0 Answers0