Android Honeycomb上的Google日历API OAuth2问题

7
我正在开发一个基于Android Honeycomb (v3.0)的应用程序,需要与Google日历API进行通信。我想让我的应用程序可以访问特定Google帐户的日历数据,以便读取和创建事件。
不幸的是,我在使用OAuth2时遇到了授权问题。以下是我的进展情况:
1)我想要访问的Google账户的日历已经注册在我正在使用的Android设备中。
2)在账户上启用了Google APIs控制台中的日历API。
3)我能够使用以下代码访问此帐户:
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) 我现在希望获取一个AuthToken来用于与日历通信。我按照这篇 教程,但把所有内容都转换为适用于Google Calendar而不是Google Tasks。通过使用getAuthTokenAUTH_TOKEN_TYPE == "oauth2:https://www.googleapis.com/auth/calendar",我成功地从AccountManager中检索到了我想要使用的帐户的authToken

5) 这里是问题开始的地方。

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) 这里是最后一行返回的异常:

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)根据这个Google API视频(等待一分钟左右以获得相关内容)的说明,这种异常发生的原因可能是我未在Google APIs控制台中为该帐户启用API访问权限。但是,如果您查看2),则可以看到我已经这样做了。

8)对我来说,问题似乎在于我无法正确设置Simple API Access Key,因为Calendar.setKey方法已被弃用。在我之前提供的Google Tasks教程中,使用Tasks.accessKey = "key"设置密钥。然而,我不确定如何在日历API中使其正常工作。我尝试过多个Google帐户,但所有帐户都出现了第5点的异常。

9)我想指出的是,使用OAuth2的传统方法对我有效。以下是我使用的代码:

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) 最后,我的问题:我想使用设备上的AccountManager的帐户来检索可用于Google日历API的工作OAuth2令牌。第二种方法对我没有用,因为用户必须手动转到他们的网络浏览器并获取授权代码,这不方便用户。有任何想法吗?抱歉发布了这么长的帖子,谢谢!


嗨,我遇到了类似的问题,我使用了这些代码片段来运行一个日历应用程序,但是我得到了403,访问被禁止的错误,请问您能否协助我,在Android中该怎么做?虽然我也没有将数字8作为选项之一。请帮忙。 - Rat-a-tat-a-tat Ratatouille
你正在使用哪个选项?你有查看 @eltrl 的答案吗? - BVB
2个回答

4
尝试将JsonHttpRequestInitializer添加到构建器中,并在那里设置您的密钥:
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();

谢谢,但是我在Eclipse中遇到了以下错误:Calendar.Builder中的setJsonHttpRequestInitializer(JsonHttpRequestInitializer)方法不适用于(new JsonHttpRequestInitializer(){})参数 - BVB
哦,我的天啊,我解决了错误(导入问题),它终于可以工作了!非常感谢你! - BVB
1
没问题,考虑到当前(beta)文档的状态,这些事情并不是很容易诊断的。 - eltrl

1
回答第10个问题:我基本上所做的都与您在处理TaskSample时所做的相同,然后使用此处提供的Android GData Calendar Sample: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 从AccountManager本身获取AuthToken:
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();
}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接