Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

IOException thrown by AccountManagerFuture.getResults whereas connection is alive

The following method systematically throws a IOException when invoked on my Android device whereas the internet connection is alive (I can retrieve emails or connect to gmail using my Android device).

Can anyone please help?

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("[email protected]");
    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);
}

Edit: Here is the stack trace:

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)

shipped jars:

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 
like image 432
balteo Avatar asked May 25 '12 16:05

balteo


1 Answers

The AccountManager is converting any network errors into IOException's. A network error could be some sort of unexpected HTTP status, so it might not be directly related to network connectivity. Note that the AccountManager supports some, but not all 'oauth2:' type tokens, so that might be related. Try with a token that is known to be supported. Also look at the logcat for hints, there might be some warnings there. Is this the full stack trace?

This works on a GN 4.0.4 (note it is using the system AccountManager, not 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);

Edit: Here's what I get when copying the token type from your post:

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".

Again, this might be my browser or smth, by check your string (esp. last part)

like image 157
Nikolay Elenkov Avatar answered Nov 11 '22 20:11

Nikolay Elenkov