如何从 Android 上保存的帐户中检索 Facebook-AuthToken

发布于 2024-10-10 09:48:12 字数 1484 浏览 0 评论 0原文

我正在尝试使用以下代码片段检索 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

听闻余生 2024-10-17 09:48:12

为什么不使用 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().

§普罗旺斯的薰衣草 2024-10-17 09:48:12

为了解释两个日志语句均未到达的事实,请考虑:

第 ~8 行:

        am.getAuthToken(account, "com.facebook.auth.login", null, ConversationList.this,

... 如果令牌立即可用,则可以返回令牌。也许这就是您正在寻找的答案?引用AccountManager文档:

如果之前生成了身份验证令牌
已为此帐户和类型缓存,
然后它被返回。否则,如果
保存的密码可用,它是
发送到服务器生成新的
身份验证令牌。否则,用户是
提示输入密码。

To explain the fact that neither of your logging statements are being reached, consider:

Line ~8:

        am.getAuthToken(account, "com.facebook.auth.login", null, ConversationList.this,

... can return a token if it's immediately available. Maybe that's the answer you're looking for? Quoting the AccountManager documentation:

If a previously generated auth token
is cached for this account and type,
then it is returned. Otherwise, if a
saved password is available, it is
sent to the server to generate a new
auth token. Otherwise, the user is
prompted to enter a password.

〆凄凉。 2024-10-17 09:48:12

请尝试调用 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.

若水般的淡然安静女子 2024-10-17 09:48:12

在 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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文