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.

4 Answers 4

6
+25

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().

6
  • 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... Jan 11, 2011 at 8: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, 2011 at 9: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, 2011 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 developers.facebook.com/docs/authentication/permissions for more info.
    – Olle
    Jan 28, 2011 at 11:25
  • 2
    @Olle The offline_access permission is now deprecated developers.facebook.com/roadmap/offline-access-removal
    – rds
    Oct 10, 2012 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.

2
  • 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... Jun 18, 2011 at 6: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, 2011 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.

9
  • 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. Jan 8, 2011 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 9, 2011 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 9, 2011 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. :( Jan 10, 2011 at 13:55
  • 1
    @dave please try to help sota if you can
    – JOE SKEET
    Jan 12, 2011 at 23:10
0

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

1
  • there is no exception thrown. if I put this am.getAuthToken inside a try everything runs through. Nothing is catched. Jan 4, 2011 at 14:12

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.