7

I'm trying to retrieve the AuthToken for Facebook (saved by Facebook for Android) by using the following piece of code.

AccountManager am = AccountManager.get(this);
Account[] accounts = am.getAccountsByType("com.facebook.auth.login");
if (accounts.length > 0) {          
    for(int j = 0; j < accounts.length; j++) {
        Account account = accounts[j];
        if(account.type != null && account.type.equals("com.facebook.auth.login")) { 
            Log.e(RuntimeVars.MY_NAME, "FACEBOOK-TYPE FOUND");
            am.getAuthToken(account, "com.facebook.auth.login", null, ConversationList.this,
                new AccountManagerCallback<Bundle>() {
                    public void run(AccountManagerFuture<Bundle> arg0) {
                        try {
                            Bundle b = arg0.getResult();
                            Log.e(RuntimeVars.MY_NAME, "THIS AUTHTOKEN: " + b.getString(AccountManager.KEY_AUTHTOKEN));
                        }
                        catch (Exception e) {
                            Log.e(RuntimeVars.MY_NAME, "EXCEPTION@AUTHTOKEN");
                        }
                    }
                }, null);
            }
        }
    }

The login credentials are found and FACEBOOK-TYPE FOUND is written into LogCat, but neither THIS AUTHTOKEN: [...] nor EXCEPTION@AUTHTOKEN is logged. So I suppose am.getAuthToken is never called.

What am I missing?

In general, if there is a better (and at least working) approach to retrieve the Facebook authtoken from the Android accounts please let me know.

Thanks a lot for your help!

Best regards
S.

Dennis Winter
  • 2,027
  • 4
  • 32
  • 45

4 Answers4

6

Why not use the Facebook SDK?

The Facebook class in it has a member to get the OAuth 2.0 access token (if that is what you need), getAccessToken().

Olle
  • 480
  • 3
  • 10
  • 1
    AFAIK users need to enter their facebook credentials if the Facebook SDK is used. But those credentials should be already available through the Accounts provider, I thought... – Dennis Winter Jan 11 '11 at 08:59
  • 1
    They should be, but may be restricted to prevent Android apps accessing user's data without explicit permission. I'd definitely suggest to go with this solution (prompting for a password when necessary), and then replace at a later date if you find a way to use AccountsManager. – Dave Jan 13 '11 at 09:27
  • 1
    AFAIK - The Facebook Android SDK returns an auth token which is valid only for 1 hour. This means that the user needs to re-login using their SDK OAUth interface everytime they want to access their FB account. – Guy Jan 27 '11 at 21:32
  • 1
    If you need a long lived token you should request the offline_access permission. This permission makes the access token returned by the OAuth endpoint long-lived. See http://developers.facebook.com/docs/authentication/permissions for more info. – Olle Jan 28 '11 at 11:25
  • thanks guys, that is really helpful. but what I don't really understand is: will my users have to actually add my app to their facebook accounts, or do I only need a facebook app to request the permissions needed? – Dennis Winter Feb 09 '11 at 09:12
  • 2
    @Olle The offline_access permission is now deprecated https://developers.facebook.com/roadmap/offline-access-removal/ – rds Oct 10 '12 at 16:52
2

To explain the fact that neither of your logging statements are being reached, consider:

Line ~8:

        am.getAuthToken(account, "com.facebook.auth.login", null, ConversationList.this,

... can return a token if it's immediately available. Maybe that's the answer you're looking for? Quoting the AccountManager documentation:

If a previously generated auth token is cached for this account and type, then it is returned. Otherwise, if a saved password is available, it is sent to the server to generate a new auth token. Otherwise, the user is prompted to enter a password.

nmr
  • 16,625
  • 10
  • 53
  • 67
  • thanks nmr. but I must say that I don't really understand why this should explain that nothing happens. :-s as it appears to me, nor new auth token is generated and neither I'm asked for the password... – Dennis Winter Jun 18 '11 at 06:58
  • 1
    If I understand correctly nothing is happening because you're not saving the result of getAuthToken. What I'm proposing as an explanation means you would have to do something like: `AccountManagerFuture amf = am.getAuthToken(account, "com.facebook.auth.login", null, ConversationList.this, ..... );` Currently you're discarding the return value, 'amf' in this example. Instead, store it and if it's non null, retrieve the auth token from it. That's what I meant anyways, not sure if that's what's actually happening. – nmr Jun 20 '11 at 16:29
1

Try calling AccountManager.blockingGetAuthToken instead. If that works, then there's something more interesting at fault here...

Also, make sure your manifest has the USE_CREDENTIALS permission set correctly.

Dave
  • 6,064
  • 4
  • 31
  • 38
  • there is null returned. Do you have any information about the authTokenTypes? I only suppose that the name for the facebook-type is "com.facebook.auth.login", but I couldn't verify that. – Dennis Winter Jan 08 '11 at 20:02
  • Well that's the first thing to confirm. I'd try calling `AccountManager.getAccountsByType(null)` to retrieve all accounts, and work from there to refine your code. Hoping the returned account data includes the information you need. It may simply not be exposed. – Dave Jan 09 '11 at 15:39
  • Apparently for Google auth the authTokenTypes string should be "ah". No idea how people determined that, but for Facebook I'd guess it's something similarly arbitrary, unfortunately. – Dave Jan 09 '11 at 15:47
  • Actually I know that the AccountType for Google is "com.google" and the TokenType is "ah", right. The AccountType for Facebook definately is "com.facebook.auth.login", but there is NO information available for Facebook's TokenType. And I find no clue how to retrieve all TokenTypes available. :( – Dennis Winter Jan 10 '11 at 13:55
  • I did. But I will try that again with your solution tomorrow morning. Thanks for your help! – Dennis Winter Jan 10 '11 at 22:30
  • Nope. No success. I'm receiving a null, whatever I try. – Dennis Winter Jan 11 '11 at 17:09
  • 1
    @dave please try to help sota if you can – JOE SKEET Jan 12 '11 at 23:10
  • Unfortunately I'm not sure what else I can suggest. – Dave Jan 13 '11 at 09:25
  • Does Facebook Authenticator work for Android from AccountManager? Has anyone in the world succeeded with this. I am never able to get it successfully. – Gopinath Dec 16 '11 at 12:46
0

add try { before am.getAuthToken and catch Exception where this method declaration ends.This will give you why and where excepption is happening

UVM
  • 9,776
  • 6
  • 41
  • 66