我在 Android 中有一个应用程序,我正在尝试使用 AccountManager 获取 AuthToken 并使用 Facebook 或 Twitter 执行操作。所以我有这个:
AccountManager am = AccountManager.get(this);
Account[] accounts = am.getAccountsByType("com.facebook.auth.login");
Bundle options = new Bundle();
Account myAccount=null;
for (int i=0;i<accounts.length;i++) {
if (accounts[i].type.equals("com.facebook.auth.login")) myAccount=accounts[i];
//options.putString("facebookUser", accounts[i].name);
}
am.getAuthToken(
myAccount, // Account retrieved using getAccountsByType()
"Manage your tasks", // Auth scope
options, // Authenticator-specific options
this, // Your activity
new OnTokenAcquired(), // Callback called when a token is successfully acquired
new Handler(new OnError()));
我的两个回调是 onTokenAcquired:
public class OnTokenAcquired implements AccountManagerCallback<Bundle> {
@Override
public void run(AccountManagerFuture<Bundle> result) {
try {
Bundle bundle = result.getResult();
Log.e("onTokenAcquired",bundle.getString(AccountManager.KEY_AUTHTOKEN));
} catch (OperationCanceledException e) {
Log.e("onTokenAcquired","operationcanceled");
} catch (AuthenticatorException e) {
Log.e("onTokenAcquired","authenticatorexception");
} catch (IOException e) {
Log.e("onTokenAcquired","IOException");
}
}
}
和 OnError:
public class OnError implements Callback {
@Override
public boolean handleMessage(Message msg) {
Log.e("onError","ERROR");
return false;
}
}
我正在关注 Android 开发人员指南 (http://developer.android.com/intl/es/training/id-auth/authenticate.html)。所以,我有两个选项,错误或获取令牌,在每个选项中我都有一个 Log.e() 来读取 SOMETHING,但没有写入。
有谁能够帮我?如果我遇到错误或没有获得令牌,至少我会有一些工作要做,但我只是不知道发生了什么。