アカウントマネージャーのクラスを使用してトークンを取得したいとします。ここでは、Twitterのためには動作しますが、facebook plzのためのサンプルコードはありません。Androidのアカウントマネージャークラスを使用してTwitterとFacebookの認証とトークンを取得する方法
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("[email protected]");
}
}
}, 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("[email protected]");
}
}
}, 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("[email protected]");
}
}
}, 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("[email protected]");
}
}
}, null);
// mHandler.sendMessageDelayed(mHandler.obtainMessage(CALL), 0);
i++;
}
}
}
-1 1つ2つの質問... Facebookのために、それはhttp://stackoverflow.com/questions/4593061/how-to-retrieve-an-facebook-authtoken-from-the-accounts-保存されたアンドロイド – rds