Using an already existing account is pretty easy, which means in the
case of Droid Facebook, Google and Exchange.  Writing the actual
account providers is the hard part.

I'm just coding this off the top of my head, since I haven't finished
writing an authenticator yet (and the SDK has no built-in accounts)
but something along the lines of this:

                /**
                NOTE: This code blocks and is the wrong way to write this if 
you are
doing it in the main thread.  In the main thread you should pass in a
callback and wait for notification of success.  For simplicity of
posting the code to this group though I have written it this way.
                **/

                AccountManager myAccountManager = AccountManager.get(this);
                Account foundGoogleAccount;
                // TODO: Do what you did to find a Google account.

                AccountManagerFuture<Bundle> myAccountManagerFuture;
                // TODO: I *think* that the Activity that getAuthToken is 
asking for
is the activity to launch the intent from, not the actual activity
that the intent will launch.  This means that the intent which leads
to a credential prompt from the user will appear on top of the passed
in Activity in the Activity stack.  However, this is not clear in the
documentation so needs testing.
                // TODO: Figure out if "com.google" account type needs any
additional login options passed in.  In this call I don't pass in any
login options but every authenticator has the option of requiring
some.  I assume this would be something like an RSA one-time-password
that can't be stored, though the authenticator could also get that via
a credential prompt instead of as a login option.  It may also be used
if a user can authenticate an account with some set of optional
parameters, such as authenticate for viewing only.
                myAccountManagerFuture = myAccountManager.getAuthToken
(foundGoogleAccount, "com.google", null, this, null, null);

                // THE FOLLOWING CALL BLOCKS WHILE AUTHENTICATION IS DONE!  
Normally
you would wait for a callback with the result, but for simplicity we
do this instead.
                Bundle myAuthTokenBundle;
                try
                {
                        myAuthTokenBundle = myAccountManagerFuture.getResult();
                }
                catch (OperationCanceledException e)
                {
                        e.printStackTrace();
                        return;
                }
                catch (AuthenticatorException e)
                {
                        e.printStackTrace();
                        return;
                }
                catch (IOException e)
                {
                        e.printStackTrace();
                        return;
                }

                if 
(myAuthTokenBundle.getString(AccountManager.KEY_ACCOUNT_TYPE) !=
"com.google")
                {
                        // TODO: Handle auth failure case.
                        return;
                }

                String myAccountName = myAuthTokenBundle.getString
(AccountManager.KEY_ACCOUNT_NAME);
                String myAccountType = myAuthTokenBundle.getString
(AccountManager.KEY_ACCOUNT_TYPE);
                // TODO: Figure out the type of auth token.  Is it always a 
String
or something?  Does it depend on the Authenticator?
                Object myGoogleAuthToken = myAuthTokenBundle.get
(AccountManager.KEY_AUTHTOKEN);


On Nov 9, 5:00 pm, Nerdrow <troybe...@gmail.com> wrote:
> I just tried this real quick on my Droid:
>
> AccountManager mgr = AccountManager.get(this);
> Account[] accts = mgr.getAccounts();
> final int count = accts.length;
> Account acct = null;
>
> for(int i=0;i<count;i++) {
>   acct = accts[i];
>   Log.d("ACCT", "eclair account - name="+acct.name+",
> type="+acct.type);
>
> }
>
> These are the results (masked :))
>
> 11-09 16:13:09.143: DEBUG/ACCT(2495): eclair account -
> name=<myemail>@gmail.com, type=com.google
> 11-09 16:13:09.143: DEBUG/ACCT(2495): eclair account -
> name=<myfacebook>@<facebook_email>.com, type=com.facebook.auth.login
> 11-09 16:13:09.143: DEBUG/ACCT(2495): eclair account -
> name=<myworkmail>@<mycompany>.com, type=com.android.exchange
>
> Do you understand how to actually use the returned Account objects?
> It looks like you have to use an AccountManager instance and call the
> getAuthToken method, but this keeps failing for me since I don't know
> the authTokenType to pass as an input parameter.  The Account object
> itself has no other useful fields as far as I can tell.
>
> Related is the AbstractAccountAuthenticator, where you're supposed to
> be able to add authenticator types to appear on the phone (by default
> on the Droid you get 3, Google, Facebook, and Exchange).  Judging by
> the documentation this would allow you to integrate your app into the
> "quick contact bar" or whatever it's called, as well as use the
> ContactsContract api to facilitate contact syncing, but there's no
> examples around any of this stuff, just the documentation.  I found
> this thread about the ContactsContract, but it doesn't help if I can't
> add my authenticator 
> first:http://groups.google.com/group/android-developers/browse_thread/threa...
>
> Anyone have any ideas?
>
> On Nov 8, 9:21 pm, Micah <mi...@zoltu.net> wrote:
>
>
>
> > In Android 2.0 there is a new android.accounts.AccountManagerclass
> > has a function getAccounts()
> > see:http://developer.android.com/reference/android/accounts/AccountManage...()
>
> > Unfortunately, as most of you know the SDK doesn't come with any of
> > the Google Apps that utilize the Google Account associated with the
> > phone (Gmail, Google Talk, Calendar, etc.).  This means that when you
> > call getAccounts in an emulator it returns an empty array.
>
> > The *hope* is that Google has modified the Google apps shipped with
> > Android 2.0 to use theAccountManagerAPI instead of whatever internal
> > and super-secret-closed system they were using before.  If this is the
> > case, then 3rd party apps can hopefully gain access to a Google Auth
> > token via theAccountManagerinstead of having to ask the user for
> > their username / password.
>
> > Unfortunately, the only phone that has android 2.0 on it to my
> > knowledge is the Motorola Droid.  So this is a call out to any
> > developers that have access to that phone to please 
> > callAccountManager.getAccounts and let us know whether or not there is a
> > Google Account on a production android device.
>
> > If not then it appears that it will be up to 3rd party developers to
> > write an Authenticator (ideally a free / open sourced one) that
> > returns a Google Auth token (and prompts / stores user's Google
> > credentials with the android.accounts API.

-- 
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to android-developers@googlegroups.com
To unsubscribe from this group, send email to
android-developers+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en

Reply via email to