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

て烟熏妆下的殇ゞ 提交于 2019-12-17 19:23:05

问题


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.


回答1:


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




回答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.




回答3:


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.




回答4:


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



来源:https://stackoverflow.com/questions/4593061/how-to-retrieve-an-facebook-authtoken-from-the-accounts-saved-on-android

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!