How to retrieve Twitter and facebook Authentication and Token through Android's Account Manager classes

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++;
        }

    }

}

Answers


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


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");

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.


Need Your Help

c - expecting struct pointer but getting struct and the reverse

c pointers gcc struct

I am attempting to call on function "play game" from main, with play game having input type 'struct player'. However, I am getting error:

NSTokenField tokens disappear when mouse leaves non-selected NSTokenField

cocoa nstokenfield

Weird UI bug that is exactly what is reported at this link: