11

I want to retrieve token via Account Manager classes. Here is sample code that works for twitter but not for facebook plz help me.

public class AccountManagerActivity extends Activity {

    AccountManager mAccountManager;
    AccountManagerFuture<Bundle> c;
    String token;


    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        mAccountManager = AccountManager.get(this);
        Account[] acc = mAccountManager.getAccounts();
        for (int i = 1; i < acc.length; i++) {
            System.out.println("Account name==" + acc[i].name);
            System.out.println("Account Type==" + acc[i].type);
        }
        AuthenticatorDescription[] ad = mAccountManager.getAuthenticatorTypes();
        for (int i = 1; i < ad.length; i++) {
            System.out.println("AuthenticatorDescription==" + ad[i].type);
        }


        tokenForTwitter();
        tokenForFacebook();
    }

    private void tokenForFacebook() {
        Account[] accts = mAccountManager
                .getAccountsByType("com.facebook.auth.login");
        int i = 0;
        if (accts.length > 0) {
            System.out.println("here");
            Account acct = accts[0];
            c = mAccountManager.getAuthToken(acct,
                    "com.facebook.auth.token" , null,
                    this, new AccountManagerCallback<Bundle>() {

                        @Override
                        public void run(AccountManagerFuture<Bundle> arg0) {
                            try {
                                Bundle b = arg0.getResult();
                                System.out.println("Facebook THIS AUHTOKEN: "
                                        + b.getString(AccountManager.KEY_AUTHTOKEN));
                                Intent launch = (Intent) b
                                        .get(AccountManager.KEY_INTENT);
                                if (launch != null) {
                                    startActivityForResult(launch, 0);
                                    return;
                                }
                            } catch (Exception e) {
                                System.out.println("EXCEPTION@AUTHTOKEN");
                            }
                        }
                    }, null);

            c = mAccountManager.getAuthToken(acct,
                    "com.facebook.auth.token.secret" /*
                                                             * what goes here
                                                             */, null, this,
                    new AccountManagerCallback<Bundle>() {

                        @Override
                        public void run(AccountManagerFuture<Bundle> arg0) {
                            try {
                                Bundle b = arg0.getResult();
                                System.out.println("Facebook THIS AUHTOKEN: "
                                        + b.getString(AccountManager.KEY_AUTHTOKEN));
                                Intent launch = (Intent) b
                                        .get(AccountManager.KEY_INTENT);
                                if (launch != null) {
                                    startActivityForResult(launch, 0);
                                    return;
                                }
                            } catch (Exception e) {
                                System.out.println("EXCEPTION@AUTHTOKEN");
                            }
                        }
                    }, null);

            // mHandler.sendMessageDelayed(mHandler.obtainMessage(CALL), 0);

            i++;
        }

    }

    public void tokenForTwitter() {
        Account[] accts = mAccountManager
                .getAccountsByType("com.twitter.android.auth.login");
        int i = 0;
        if (accts.length > 0) {
            System.out.println("here");
            Account acct = accts[0];
            c = mAccountManager.getAuthToken(acct,
                    "com.twitter.android.oauth.token" /* what goes here */, null,
                    this, new AccountManagerCallback<Bundle>() {

                        @Override
                        public void run(AccountManagerFuture<Bundle> arg0) {
                            try {
                                Bundle b = arg0.getResult();
                                System.out.println("twitter THIS AUHTOKEN: "
                                        + b.getString(AccountManager.KEY_AUTHTOKEN));
                                Intent launch = (Intent) b
                                        .get(AccountManager.KEY_INTENT);
                                if (launch != null) {
                                    startActivityForResult(launch, 0);
                                    return;
                                }
                            } catch (Exception e) {
                                System.out.println("EXCEPTION@AUTHTOKEN");
                            }
                        }
                    }, null);

            c = mAccountManager.getAuthToken(acct,
                    "com.twitter.android.oauth.token.secret" /*
                                                             * what goes here
                                                             */, null, this,
                    new AccountManagerCallback<Bundle>() {

                        @Override
                        public void run(AccountManagerFuture<Bundle> arg0) {
                            try {
                                Bundle b = arg0.getResult();
                                System.out.println("twitter THIS AUHTOKEN: "
                                        + b.getString(AccountManager.KEY_AUTHTOKEN));
                                Intent launch = (Intent) b
                                        .get(AccountManager.KEY_INTENT);
                                if (launch != null) {
                                    startActivityForResult(launch, 0);
                                    return;
                                }
                            } catch (Exception e) {
                                System.out.println("EXCEPTION@AUTHTOKEN");
                            }
                        }
                    }, null);

            // mHandler.sendMessageDelayed(mHandler.obtainMessage(CALL), 0);

            i++;
        }

    }

}
  • -1 Two questions in one... For Facebook, it is a duplicate of http://stackoverflow.com/questions/4593061/how-to-retrieve-an-facebook-authtoken-from-the-accounts-saved-on-android – rds Oct 10 '12 at 16:50

3 Answers3

8

Call AccountManager.getAccountsByType(null) to retrieve all accounts, and check the returned account data includes the information you need. It may simply not be exposed.

Try calling AccountManager.blockingGetAuthToken instead. Also, make sure your manifest has the USE_CREDENTIALS permission set correctly.

You can see this discussion How to retrieve an Facebook-AuthToken from the accounts saved on Android

But I would also suggest Facebook SDK with offline access permission(This permission makes the access token returned by the OAuth endpoint long-lived, otherwise auth token is valid only for 1 hour.)

Community
  • 1
  • 1
Somnath Muluk
  • 55,015
  • 38
  • 216
  • 226
2

You can also create intent and obtain token from facebook application

Intent intent = new Intent();
intent.setClassName("com.facebook.katana", "com.facebook.katana.ProxyAuth");
intent.putExtra("client_id", apiKey);
intent.putExtra("scope", scope);

try {
    activity.startActivityForResult(intent, requestCode);
} catch (ActivityNotFoundException e) {
    return false;
}

Then onActivityResult(int requestCode, int resultCode, Intent data) of you activity you can get the token using

data.getStringExtra("access_token");
pleerock
  • 18,322
  • 16
  • 103
  • 128
0

Just for information, the facebook application part of getAuthToken is not implemented. When you decompile it, you see that it just returns null.

You should use the Facebook SDK.

Antoine Martin
  • 1,137
  • 10
  • 6