【发布时间】:2011-12-08 17:42:27
【问题描述】:
我正在开发一个需要与 Google Calendar API 通信的 Android Honeycomb (v3.0) 应用程序。我想允许我的应用程序访问特定 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 以在与日历通信时使用。我关注了这个tutorial,但将所有内容都转换为使用谷歌日历而不是谷歌任务。我使用getAuthToken 和AUTH_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 Video(等待一分钟左右以获取适用的内容),此异常的一个原因可能是我没有在 Google API 控制台中为该帐户启用 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 中使用该帐户,以便检索一个有效的 OAuth2 令牌,以便与 Google Calendar API 一起使用。第二种方法对我没有用,因为用户必须手动访问他们的网络浏览器并获取授权码,这对用户不友好。有人有想法么?很抱歉发了这么长的帖子,谢谢!
【问题讨论】:
-
嗨,我遇到了类似的问题,我使用此代码 sn-ps 来运行日历应用程序,但随后出现 403,访问禁止错误,请您帮忙,我应该怎么做在安卓里做吗?虽然我也没有使用数字 8 作为选项。请帮忙。
-
您使用什么选项?你检查过@eltrl 的答案吗?
标签: android google-api android-3.0-honeycomb google-calendar-api google-api-java-client