How to retrieve an Facebook-AuthToken from the accounts saved on Android
Asked Answered
D

4

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.

Destalinization answered 4/1, 2011 at 11:24 Comment(0)
T
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().

Taneka answered 10/1, 2011 at 16:14 Comment(6)
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...Destalinization
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.Showy
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.Perishing
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.Taneka
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?Destalinization
@Taneka The offline_access permission is now deprecated developers.facebook.com/roadmap/offline-access-removalMcinnis
S
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.

Shackleton answered 17/6, 2011 at 22:53 Comment(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...Destalinization
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.Shackleton
S
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.

Showy answered 8/1, 2011 at 15:25 Comment(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.Destalinization
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.Showy
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.Showy
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. :(Destalinization
I did. But I will try that again with your solution tomorrow morning. Thanks for your help!Destalinization
Nope. No success. I'm receiving a null, whatever I try.Destalinization
@dave please try to help sota if you canSubpoena
Unfortunately I'm not sure what else I can suggest.Showy
Does Facebook Authenticator work for Android from AccountManager? Has anyone in the world succeeded with this. I am never able to get it successfully.Weihs
E
0

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

Emanuelemanuela answered 4/1, 2011 at 13:12 Comment(1)
there is no exception thrown. if I put this am.getAuthToken inside a try everything runs through. Nothing is catched.Destalinization

© 2022 - 2024 — McMap. All rights reserved.