Android Honeycomb 上的 Google 日历 API OAuth2 问题

发布于 2024-12-20 09:30:36 字数 4092 浏览 4 评论 0原文

我正在开发一个 Android Honeycomb (v3.0) 应用程序,该应用程序需要与 Google Calendar API 进行通信。我希望允许我的应用程序访问特定 Google 帐户的日历数据,以便读取和创建事件。

不幸的是,我遇到了使用 OAuth2 授权的问题。到目前为止,我所拥有的内容如下:

1) 我想要访问其日历的 Google 帐户已在我正在使用的 Android 设备中注册。

2) 我在帐户的 Google API 控制台中启用了日历 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 日历而不是 Google 任务。我通过将 getAuthTokenAUTH_TOKEN_TYPE == "oauth2: https://www.googleapis.com/auth/calendar”

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 视频(请稍等一分钟才能看到适用的内容),出现此异常的原因可能是我没有启用 API访问帐户的 Google API 控制台。然而,如果你看一下2),你就会发现我确实这样做了。

8) 对我来说,问题似乎是我无法正确设置简单 API 访问密钥,因为 Calendar.setKey 方法已被弃用。在我之前链接的 Google Tasks 教程中,密钥是使用 Tasks.accessKey = "key" 设置的。不过,我不确定如何使用 Calendar 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 Calendar API 一起使用的工作 OAuth2 令牌。第二种方法对我来说没有用,因为用户必须手动转到网络浏览器并获取授权码,这对用户不友好。有人有什么想法吗?对于这么长的帖子表示歉意,谢谢!

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!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

携君以终年 2024-12-27 09:30:36

尝试将 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();

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();
山色无中 2024-12-27 09:30:36

回答第 10 个问题:我基本上必须执行您必须使用 TaskSample 执行的操作,然后使用此处提供的 Android GData 日历示例:http://code.google.com/p/google-api-java-client/source/browse/calendar-android-sample/src/main/java/com/google/api/client/样本/日历/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();
}

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();
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文