简体   繁体   中英

Authenticating with Google Calendar API

I'm writing an Android app that uses both the Google Tasks and Calendar API. Authenticating through the Tasks works perfectly. But, when accessing the calendar API, I get a screen that says "A problem occurred while communicating with Google services. Please try again later." with the heading "Couldn't sign in". If I click next, a signing in... screen is displayed for about half a second, then I'm sent back to the "problem occurred" screen.

The code where I authenticate to the Calendar API is below. Note that the code for the Tasks API, which works fine, is almost identical.

GoogleAccountManager google_manager = new GoogleAccountManager(this);
Account[] accounts = google_manager.getAccounts();
Account my_account;
if (accounts.length > 1) my_account = selectWhichAccount(accounts);
else {
    if (accounts.length == 0) display_no_account_dialog();
    Log.v("account",accounts[0].toString());
    my_account = accounts[0];
}
final String AUTH_TOKEN_TYPE = "oauth2:https://www.googleapis.com/auth/calendar";
Log.v("schedule","getting auth token");
google_manager.manager.getAuthToken(my_account, AUTH_TOKEN_TYPE, null, this, new AccountManagerCallback<Bundle>() {

        @Override
        public void run(AccountManagerFuture<Bundle> future) {
            try {
                String token = future.getResult().getString(AccountManager.KEY_AUTHTOKEN);
                Log.d("schedule","starting scheduletask");
                start_ScheduleTask(t, token);
            } catch (OperationCanceledException e) {
                Log.v("accounts","denied access to account");
                handleException(e);
            } catch (Exception e) {
                handleException(e);
            }
        }
    }, null);

OAuth2 tokens specified using the 'oauth2:' prefix are only partially supported by AccountManager and some of them don't work. Unfortunately, what is supported and what is not is not currently documented (AFAIK). You have two alternatives: use ClientLogin (use 'cl' as the token type) or get a token using a WebView. The ClientLogin should work fine for now, however it is officially deprecated and will be going away in about a year.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM