我想通过Account Manager类检索令牌。这是适用于Twitter的示例代码,但不适用于facebook plz帮助我。
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++;
}
}
}
答案 0 :(得分:8)
致电AccountManager.getAccountsByType(null)
以检索所有帐户,并检查返回的帐户数据是否包含您需要的信息。它可能根本就没有曝光。
请尝试拨打AccountManager.blockingGetAuthToken
。另外,请确保您的清单正确设置了USE_CREDENTIALS
权限。
您可以看到此讨论How to retrieve an Facebook-AuthToken from the accounts saved on Android
但我还建议Facebook SDK
具有离线访问权限(此权限使OAuth端点返回的访问令牌持久,否则身份验证令牌仅在1小时内有效。)
答案 1 :(得分:2)
您还可以创建意图并从Facebook应用程序获取令牌
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;
}
然后onActivityResult(int requestCode, int resultCode, Intent data)
您的活动,您可以使用
data.getStringExtra("access_token");
答案 2 :(得分:0)
仅供参考,getAuthToken的facebook应用程序部分未实现。反编译时,您会看到它只返回null。
您应该使用Facebook SDK。