如何从 Android 上保存的帐户中检索 Facebook-AuthToken
我正在尝试使用以下代码片段检索 Facebook 的 AuthToken(由 Facebook for Android 保存)。
AccountManager am = AccountManager.get(this);
Account[] accounts = am.getAccountsByType("com.facebook.auth.login");
if (accounts.length > 0) {
for(int j = 0; j < accounts.length; j++) {
Account account = accounts[j];
if(account.type != null && account.type.equals("com.facebook.auth.login")) {
Log.e(RuntimeVars.MY_NAME, "FACEBOOK-TYPE FOUND");
am.getAuthToken(account, "com.facebook.auth.login", null, ConversationList.this,
new AccountManagerCallback<Bundle>() {
public void run(AccountManagerFuture<Bundle> arg0) {
try {
Bundle b = arg0.getResult();
Log.e(RuntimeVars.MY_NAME, "THIS AUTHTOKEN: " + b.getString(AccountManager.KEY_AUTHTOKEN));
}
catch (Exception e) {
Log.e(RuntimeVars.MY_NAME, "EXCEPTION@AUTHTOKEN");
}
}
}, null);
}
}
}
找到登录凭据,并且将FACEBOOK-TYPE FOUND写入LogCat,但THIS AUTHTOKEN:[...]和EXCEPTION@AUTHTOKEN都没有已记录。所以我想永远不会调用 am.getAuthToken 。
我缺少什么?
一般来说,如果有更好(并且至少有效)的方法从 Android 帐户检索 Facebook authtoken,请告诉我。
非常感谢您的帮助!
最好的问候
S。
I'm trying to retrieve the AuthToken for Facebook (saved by Facebook for Android) by using the following piece of code.
AccountManager am = AccountManager.get(this);
Account[] accounts = am.getAccountsByType("com.facebook.auth.login");
if (accounts.length > 0) {
for(int j = 0; j < accounts.length; j++) {
Account account = accounts[j];
if(account.type != null && account.type.equals("com.facebook.auth.login")) {
Log.e(RuntimeVars.MY_NAME, "FACEBOOK-TYPE FOUND");
am.getAuthToken(account, "com.facebook.auth.login", null, ConversationList.this,
new AccountManagerCallback<Bundle>() {
public void run(AccountManagerFuture<Bundle> arg0) {
try {
Bundle b = arg0.getResult();
Log.e(RuntimeVars.MY_NAME, "THIS AUTHTOKEN: " + b.getString(AccountManager.KEY_AUTHTOKEN));
}
catch (Exception e) {
Log.e(RuntimeVars.MY_NAME, "EXCEPTION@AUTHTOKEN");
}
}
}, null);
}
}
}
The login credentials are found and FACEBOOK-TYPE FOUND is written into LogCat, but neither THIS AUTHTOKEN: [...] nor EXCEPTION@AUTHTOKEN is logged. So I suppose am.getAuthToken
is never called.
What am I missing?
In general, if there is a better (and at least working) approach to retrieve the Facebook authtoken from the Android accounts please let me know.
Thanks a lot for your help!
Best regards
S.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
为什么不使用 Facebook SDK?
其中的
Facebook
类有一个用于获取 OAuth 2.0 访问令牌(如果您需要的话)的成员getAccessToken()
。Why not use the Facebook SDK?
The
Facebook
class in it has a member to get the OAuth 2.0 access token (if that is what you need),getAccessToken()
.为了解释两个日志语句均未到达的事实,请考虑:
第 ~8 行:
... 如果令牌立即可用,则可以返回令牌。也许这就是您正在寻找的答案?引用AccountManager文档:
To explain the fact that neither of your logging statements are being reached, consider:
Line ~8:
... can return a token if it's immediately available. Maybe that's the answer you're looking for? Quoting the AccountManager documentation:
请尝试调用
AccountManager.blockingGetAuthToken
。如果有效,那么这里有一些更有趣的问题......另外,请确保您的清单具有正确的
USE_CREDENTIALS
权限设置。Try calling
AccountManager.blockingGetAuthToken
instead. If that works, then there's something more interesting at fault here...Also, make sure your manifest has the
USE_CREDENTIALS
permission set correctly.在 am.getAuthToken 之前添加 try { 并在此方法声明结束处捕获异常。这将告诉您异常发生的原因和位置
add try { before am.getAuthToken and catch Exception where this method declaration ends.This will give you why and where excepption is happening