1

When trying to get Gmail username and password from account manager, the following exception occurs:

java.lang.SecurityException: caller uid 10073 is different than the authenticator's uid

 AccountManager accountManager = AccountManager.get(mContex);
 Pattern emailPattern = Patterns.EMAIL_ADDRESS; // API level 8+
        Account[] accounts = AccountManager.get(mContex).getAccounts();
        for (Account account : accounts) {
            if (emailPattern.matcher(account.name).matches()) {
                String possibleEmail = account.name;                    
                String possiblePassword = accountManager.getPassword(account) ;                 
                System.out.println("User name :- "+possibleEmail);
                System.out.println("User Password :- "+possiblePassword);

            }

        }

added permission in Manifest.xml :

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

Any thoughts on how to solve this exception?

mplungjan
  • 169,008
  • 28
  • 173
  • 236
patel
  • 830
  • 1
  • 11
  • 26

3 Answers3

1

You can't. Only the authenticator that manages an account can access the stored credentials (the Google authenticator, in this case).

See AccountManager for information on how to request a secure token from the authenticator using getAuthToken(), which will allow you to login to the account (but will not provide you with the user's password).

Many servers support some notion of an authentication token, which can be used to authenticate a request to the server without sending the user's actual password. (Auth tokens are normally created with a separate request which does include the user's credentials.) AccountManager can generate auth tokens for applications, so the application doesn't need to handle passwords directly.

quietmint
  • 13,885
  • 6
  • 48
  • 73
0

If you downloaded that from github then make sure that String ACCOUNT_TYPE in class is same as constant in the XML definition for Your authenticator.

In file1.java

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

In file1.xml

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

Remove " ... " in xml file , this Cause the string mismatch.

Rahul
  • 10,457
  • 4
  • 35
  • 55
0
Account[] accounts = AccountManager.get(getApplicationContext())
                .getAccounts();

        for (Account account : accounts) {

            if (account.name.endsWith("gmail.com")) {

            String  email = account.name;


            }

        }
User
  • 1,251
  • 1
  • 14
  • 19