在Android蜂巢谷歌日历API OAuth2故障 [英] Google Calendar API OAuth2 Troubles on Android Honeycomb

查看:234
本文介绍了在Android蜂巢谷歌日历API OAuth2故障的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我工作的一个Android蜂窝(3.0)应用程序已经与谷歌日历API通信的要求。我想,让我的应用程序访问特定的谷歌帐户的日历数据,以便读取和创建活动。

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.

不幸的是,我遇到了一个问题,授权使用OAuth2。这是我到目前为止有:

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

1)谷歌帐户的日历,我想访问我有工作在Android设备中被注册。

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

2)我启用了谷歌API控制台中的日历API的帐户。

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

3)我可以用下面的code访问该帐户:

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)我现在想与日历通信时获得的authToken使用。我跟着这个教程,但转换的一切与谷歌日历,而不是谷歌的任务工作。我成功地检索的authToken 的AccountManager 与账号,我想通过使用 getAuthToken AUTH_TOKEN_TYPE ==oauth2如下:https://www.googleapis.com/auth/calendar

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)这里就是问题的开始。我现在在这一点上:

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

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)根据本谷歌API视频(等待分钟左右才能到适用的内容),有原因的异常可能是事实,我没有启用该帐户在谷歌API控制台中的API访问。但是,如果你2),你可以看到,我没有这样做。

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)对我来说,似乎问题是,我无法正确设置的简单API访问键,因为 Calendar.setKey 法德precated。内谷歌的任务教程,我previously联系,关键是使用 Tasks.accessKey =键设置。我不知道如何得到这个工作的日历API,虽然。我曾尝试多个谷歌帐户,而这一切想出除5)。

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)我想指出的是,使用OAuth2的传统方法做的工作对我来说。这里的code我用的是:

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)最后,我的问题:我想使用的帐户从的AccountManager在设备上,以便检索工作OAuth2令牌与谷歌日历API的使用。第二种方法是不适合我用,因为用户必须手动去他们的网页浏览器,并获得授权,code,这是不人性化。任何人有什么想法?道歉长职务,并感谢!

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!

推荐答案

尝试添加JsonHtt prequestInitializer的建设者和设置您的主要有:

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();

这篇关于在Android蜂巢谷歌日历API OAuth2故障的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆