Google Calendar API OAuth2 Troubles on Android Honeycomb

南笙酒味 提交于 2019-12-29 05:28:08

问题


I am working on an Android Honeycomb (v3.0) application that has a requirement of communicating with the Google Calendar API. I would like to allow my application to access a particular Google account's Calendar data in order to read and create events.

Unfortunately, I ran into a problem with authorization using OAuth2. Here's what I have so far:

1) The Google account whose calendar I would like to access is registered within the Android device I am working with.

2) I enabled the Calendar API within the Google APIs Console on the account.

3) I am able to access this account using the following code:

AccountManager accountManager = AccountManager.get(this.getBaseContext());
Account[] accounts = accountManager.getAccountsByType("com.google");
Account acc = accounts[0]; // The device only has one account on it

4) I would now like to obtain an AuthToken for use when communicating with the calendar. I followed this tutorial, but converted everything to work with Google Calendar instead of Google Tasks. I successfully retrieve an authToken from the AccountManager with the account I would like to use by using getAuthToken with AUTH_TOKEN_TYPE == "oauth2:https://www.googleapis.com/auth/calendar".

5) Here's where the problems begin. I am now at this point:

AccessProtectedResource accessProtectedResource = new GoogleAccessProtectedResource(tokens[0]); // this is the correct token
HttpTransport transport = AndroidHttp.newCompatibleTransport();
Calendar service = Calendar.builder(transport, new JacksonFactory())
    .setApplicationName("My Application's Name")
    .setHttpRequestInitializer(accessProtectedResource)
    .build();
service.setKey("myCalendarSimpleAPIAccessKey"); // This is deprecated???
Events events = service.events().list("primary").execute(); // Causes an exception!

6) Here's the exception returned by the last line:

com.google.api.client.googleapis.json.GoogleJsonResponseException: 403 Forbidden
{
  "code" : 403,
  "errors" : [ {
    "domain" : "usageLimits",
    "message" : "Daily Limit Exceeded. Please sign up",
    "reason" : "dailyLimitExceededUnreg",
    "extendedHelp" : "https://code.google.com/apis/console"
  } ],
  "message" : "Daily Limit Exceeded. Please sign up"
}

7) According to this Google API Video (wait a minute or so to get to the applicable content), a reason for this exception may be the fact that I did not enable the API access within the Google APIs Console for the account. However, if you look at 2), you can see that I did do so.

8) To me, it seems that the problem is that I was unable to set the Simple API Access Key correctly, because the Calendar.setKey method is deprecated. Within the Google Tasks tutorial that I previously linked, the key is set using Tasks.accessKey = "key". I'm not sure how to get this working with the Calendar API, though. I have tried multiple Google accounts, which all came up with the exception from 5).

9) I would like to point out that the traditional method of using OAuth2 did work for me. Here's the code I used for that:

HttpTransport TRANSPORT = new NetHttpTransport();
JsonFactory JSON_FACTORY = new JacksonFactory();
String SCOPE = "https://www.googleapis.com/auth/calendar";
String CALLBACK_URL = "urn:ietf:wg:oauth:2.0:oob";
String CLIENT_ID = "myClientID";
String CLIENT_SECRET = "myClientSecret";
String authorizeUrl = new GoogleAuthorizationRequestUrl(CLIENT_ID, CALLBACK_URL, SCOPE).build();
String authorizationCode = "???"; // At this point, I have to manually go to the authorizeUrl and grab the authorization code from there to paste it in here while in debug mode

GoogleAuthorizationCodeGrant authRequest = new GoogleAuthorizationCodeGrant(TRANSPORT, JSON_FACTORY, CLIENT_ID, CLIENT_SECRET, authorizationCode, CALLBACK_URL);
authRequest.useBasicAuthorization = false;
AccessTokenResponse authResponse = authRequest.execute();
String accessToken = authResponse.accessToken; // gets the correct token

GoogleAccessProtectedResource access = new GoogleAccessProtectedResource(accessToken, TRANSPORT, JSON_FACTORY, CLIENT_ID, CLIENT_SECRET, authResponse.refreshToken);
HttpRequestFactory rf = TRANSPORT.createRequestFactory(access);
AccessProtectedResource accessProtectedResource = new GoogleAccessProtectedResource(accessToken);
HttpTransport transport = AndroidHttp.newCompatibleTransport();

Calendar service = Calendar.builder(transport, new JacksonFactory())
    .setApplicationName("My Application's Name")
    .setHttpRequestInitializer(accessProtectedResource)
    .build();

Events events = service.events().list("primary").execute(); // this works!

10) Finally, my question: I would like to use the account from the AccountManager on the device itself in order to retrieve a working OAuth2 token for use with the Google Calendar API. The second method is not useful for me, because the user will have to manually go to their web browser and get the authorization code, which is not user friendly. Anyone have any ideas? Apologies for the long post, and thanks!


回答1:


Try adding a JsonHttpRequestInitializer to the builder and setting your key there:

Calendar service = Calendar.builder(transport, new JacksonFactory())
.setApplicationName("My Application's Name")
.setHttpRequestInitializer(accessProtectedResource)
.setJsonHttpRequestInitializer(new JsonHttpRequestInitializer() {
    public void initialize(JsonHttpRequest request) {
        CalendarRequest calRequest = (CalendarRequest) request;
        calRequest.setKey("myCalendarSimpleAPIAccessKey");
    }

}).build();



回答2:


To answer no 10 : I've basically had to do what you had to do working with the TaskSample and then use the Android GData Calendar Sample available here : http://code.google.com/p/google-api-java-client/source/browse/calendar-android-sample/src/main/java/com/google/api/client/sample/calendar/android/CalendarSample.java?repo=samples to get the AuthToken from the AccountManager itself:

accountManager = new GoogleAccountManager(this);
settings = this.getSharedPreferences(PREF, 0);
gotAccount();

private void gotAccount() {
        Account account = accountManager.getAccountByName(accountName);
        if (account != null) {
            if (settings.getString(PREF_AUTH_TOKEN, null) == null) {
                accountManager.manager.getAuthToken(account, AUTH_TOKEN_TYPE,
                        true, new AccountManagerCallback<Bundle>() {

                            @Override
                            public void run(AccountManagerFuture<Bundle> future) {
                                try {
                                    Bundle bundle = future.getResult();
                                    if (bundle
                                            .containsKey(AccountManager.KEY_INTENT)) {
                                        Intent intent = bundle
                                                .getParcelable(AccountManager.KEY_INTENT);
                                        int flags = intent.getFlags();
                                        flags &= ~Intent.FLAG_ACTIVITY_NEW_TASK;
                                        intent.setFlags(flags);
                                        startActivityForResult(intent,
                                                REQUEST_AUTHENTICATE);
                                    } else if (bundle
                                            .containsKey(AccountManager.KEY_AUTHTOKEN)) {
                                        setAuthToken(bundle
                                                .getString(AccountManager.KEY_AUTHTOKEN));
                                        // executeRefreshCalendars();
                                    }
                                } catch (Exception e) {
                                    handleException(e);
                                }
                            }
                        }, null);
            } else {
                // executeRefreshCalendars();
            }
            return;
        }
        chooseAccount();
    }

private void chooseAccount() {
    accountManager.manager.getAuthTokenByFeatures(
            GoogleAccountManager.ACCOUNT_TYPE, AUTH_TOKEN_TYPE, null,
            ExportClockOption.this, null, null,
            new AccountManagerCallback<Bundle>() {

                @Override
                public void run(AccountManagerFuture<Bundle> future) {
                    Bundle bundle;
                    try {
                        bundle = future.getResult();
                        setAccountName(bundle
                                .getString(AccountManager.KEY_ACCOUNT_NAME));
                        setAuthToken(bundle
                                .getString(AccountManager.KEY_AUTHTOKEN));
                        // executeRefreshCalendars();
                    } catch (OperationCanceledException e) {
                        // user canceled
                    } catch (AuthenticatorException e) {
                        handleException(e);
                    } catch (IOException e) {
                        handleException(e);
                    }
                }
            }, null);
}

void setAuthToken(String authToken) {
    SharedPreferences.Editor editor = settings.edit();
    editor.putString(PREF_AUTH_TOKEN, authToken);
    editor.commit();
    createCalendarService(authToken);
    try {
        Events events = service.events().list("primary").execute();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

}

private void createCalendarService(String authToken) {
    accessProtectedResource = new GoogleAccessProtectedResource(authToken);

    Log.i(TAG, "accessProtectedResource.getAccessToken() = "
            + accessProtectedResource.getAccessToken());
    JacksonFactory jsonFactory = new JacksonFactory();
    service = com.google.api.services.calendar.Calendar
            .builder(transport, jsonFactory)
            .setApplicationName("Time Journal")
            .setJsonHttpRequestInitializer(
                    new JsonHttpRequestInitializer() {
                        @Override
                        public void initialize(JsonHttpRequest request) {
                            CalendarRequest calendarRequest = (CalendarRequest) request;
                            calendarRequest
                                    .setKey("<YOUR SIMPLE API KEY>");
                        }
                    }).setHttpRequestInitializer(accessProtectedResource)
            .build();
}


来源:https://stackoverflow.com/questions/8435228/google-calendar-api-oauth2-troubles-on-android-honeycomb

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!