0

Well i need to authorize Google Calendar's access for a user, first google Id works fine when i use

blockingGetAuthToken

and it gets a token, i usually log on this token. So when i tried to use other accounts i got a null token. I searched a lot and found out that using getAuthToken is preferred as it uses a context from the activity calling it.. then i converted the whole process to use it

 private static final String AUTH_TOKEN_TYPE = "oauth2:https://www.googleapis.com/auth/calendar";

public static String authorize(AndroidtestActivity parent, Account account) {
    AccountManager accountManager = AccountManager.get(parent);
    Bundle options= new Bundle();

    Log.d("MyAPP", "Get Authorization");
    try {
        AccountManagerFuture<Bundle> acc=accountManager.getAuthToken ( account, AUTH_TOKEN_TYPE,  options, true, null, null);
        Bundle authTokenBundle = acc.getResult();
        String authToken = authTokenBundle.get(AccountManager.KEY_AUTHTOKEN).toString();

        Log.d("MyAPP","Token= "+authToken);
        return authToken;


    } catch (Exception ex) {
        Logger.getLogger(GoogleAuthorize.class.getName()).log(Level.SEVERE,
                null, ex);
    }
    return null;
}

}

but still no accounts could get a valid token, they all get a null one

then i saw this answer https://stackoverflow.com/a/2021337/1280902 and followed using invalidateAuthToken

private static final String AUTH_TOKEN_TYPE = "oauth2:https://www.googleapis.com/auth/calendar";

public static String authorize(AndroidtestActivity parent, Account account) {
    AccountManager accountManager = AccountManager.get(parent);
    Bundle options= new Bundle();

    Log.d("MyAPP", "Get Authorization");
    try {
        AccountManagerFuture<Bundle> acc=accountManager.getAuthToken ( account, AUTH_TOKEN_TYPE,  options, true, null, null);
        Bundle authTokenBundle = acc.getResult();
        String authToken = authTokenBundle.get(AccountManager.KEY_AUTHTOKEN).toString();
        accountManager.invalidateAuthToken("com.google",authToken);
        acc=accountManager.getAuthToken ( account, AUTH_TOKEN_TYPE,  options, true, null, null);
        authTokenBundle = acc.getResult();
        authToken = authTokenBundle.get(AccountManager.KEY_AUTHTOKEN).toString();

        Log.d("MyAPP","Token= "+authToken);
        return authToken;


    } catch (Exception ex) {
        Logger.getLogger(GoogleAuthorize.class.getName()).log(Level.SEVERE,
                null, ex);
    }
    return null;
}

}

but i had the same problem on every account i use, even the one that used to work at the beginning with blockingGetAuthToken

So am i missing something?

Community
  • 1
  • 1
Tar3k
  • 51
  • 1
  • 9
  • 1
    Just a small hint: you can use the string "Manage your calendars" as AUTH_TOKEN_TYPE. This will appear on the user confirmation screen for granting access instead of that obscure URL. It took me a while to figure that one out. Documentation is really sparse about that. – tiguchi Jul 07 '12 at 16:44
  • Ohh thank you.. I had to search for those Strings instead of URL, but google didn't update their docs (as usual) Thank you @NobuGames ,Appreciate your help – Tar3k Jul 07 '12 at 16:46

1 Answers1

2

Ok it works fine when i use

getAuthToken (Account account, String authTokenType, Bundle options, Activity activity, AccountManagerCallback<Bundle> callback, Handler handler)

The activity parameter solved the problem..

Tar3k
  • 51
  • 1
  • 9