AccountManagerFuture.getResults抛出IOException,而连接仍然存在

时间:2012-05-25 16:38:20

标签: android oauth-2.0 access-token google-account

以下方法在我的Android设备上调用时系统地抛出IOException,而互联网连接仍然存在(我可以使用我的Android设备检索电子邮件或连接到gmail)。

有人可以帮忙吗?

private void performAuthentication() {
    Log.d("GAWidget", "performAuthentication");
    GoogleCredential credential = new GoogleCredential();
    GoogleAccountManager accountManager = new GoogleAccountManager(this);
    Log.d("GAWidget", "after getting accountManager");
    Account account = accountManager.getAccountByName("balteo@gmail.com");
    Log.d("GAWidget", "after getting account"+"account.name: "+account.name);
    accountManager.getAccountManager().getAuthToken(account, "oauth2:https://www.googleapis.com/auth/‌​analytics.readonly",
            true, new AccountManagerCallback<Bundle>() {

                public void run(AccountManagerFuture<Bundle> future) {
                    try {
                        String token = future.getResult(15, TimeUnit.SECONDS).getString(AccountManager.KEY_AUTHTOKEN);
                        Log.d("GAWidget", "token: "+token);
                        useToken(token);
                    } catch (OperationCanceledException e) {
                        Log.e("GAWidget", "OperationCanceledException", e);
                    } catch (AuthenticatorException e) {
                        Log.e("GAWidget", "AuthenticatorException", e);
                    } catch (IOException e) {
                        Log.e("GAWidget", "IOException", e);
                    }
                }

            }, null);
}

编辑:这是堆栈跟踪:

05-27 19:09:04.319: E/GAWidget(12487): IOException
05-27 19:09:04.319: E/GAWidget(12487): java.io.IOException
05-27 19:09:04.319: E/GAWidget(12487):  at android.accounts.AccountManager.convertErrorToException(AccountManager.java:1440)
05-27 19:09:04.319: E/GAWidget(12487):  at android.accounts.AccountManager.access$400(AccountManager.java:138)
05-27 19:09:04.319: E/GAWidget(12487):  at android.accounts.AccountManager$AmsTask$Response.onError(AccountManager.java:1301)
05-27 19:09:04.319: E/GAWidget(12487):  at android.accounts.IAccountManagerResponse$Stub.onTransact(IAccountManagerResponse.java:69)
05-27 19:09:04.319: E/GAWidget(12487):  at android.os.Binder.execTransact(Binder.java:320)
05-27 19:09:04.319: E/GAWidget(12487):  at dalvik.system.NativeStart.run(Native Method)

装运罐子:

google-http-client-1.9.0-beta.jar
google-http-client-android2-1.9.0-beta.jar (only for SDK >= 2.1)
google-http-client-android3-1.9.0-beta.jar (only for SDK >= 3.0)
gson-2.1.jar
guava-11.0.1.jar
jackson-core-asl-1.9.4.jar
jsr305-1.3.9.jar
protobuf-java-2.2.0.jar 

1 个答案:

答案 0 :(得分:7)

AccountManager正在将任何网络错误转换为IOException。网络错误可能是某种意外的HTTP状态,因此它可能与网络连接没有直接关系。请注意,AccountManager支持一些但不是所有'oauth2:'类型的令牌,因此可能是相关的。尝试使用已知支持的令牌。另外看一下logcat的提示,可能会有一些警告。这是完整的堆栈跟踪吗?

这适用于GN 4.0.4(注意它使用的是系统AccountManager,而不是GoogleAccountManager):

    AccountManager am = AccountManager.get(this);
    Account[] accounts = accountManager.getAccountsByType("com.google");
    String AUTH_TOKEN_TYPE = "oauth2:https://www.googleapis.com/auth/analytics.readonly";
    am.getAuthToken(accounts[0], AUTH_TOKEN_TYPE, null,
                   this, new AccountManagerCallback<Bundle>() {
                        public void run(AccountManagerFuture<Bundle> future) {
                           try {
                             String token = 
future.getResult().getString(AccountManager.KEY_AUTHTOKEN);
                            System.out.println("token " + token);
                            } catch (...) {}

                            }
                        }, null);

编辑:这是我从你的帖子中复制令牌类型时得到的结果:

0000000: 226f 6175 7468 323a 6874 7470 733a 2f2f  "oauth2:https://
0000010: 7777 772e 676f 6f67 6c65 6170 6973 2e63  www.googleapis.c
0000020: 6f6d 2f61 7574 682f 3f3f 616e 616c 7974  om/auth/??analyt
0000030: 6963 732e 7265 6164 6f6e 6c79 220a       ics.readonly".

同样,这可能是我的浏览器或smth,通过检查你的字符串(尤其是最后一部分)