Android Calendar API上的Google Calendar API OAuth2令人烦恼

BVB*_*BVB 7 android google-calendar-api google-api google-api-java-client android-3.0-honeycomb

我正在开发Android Honeycomb(v3.0)应用程序,该应用程序需要与Google Calendar API进行通信.我想允许我的应用访问特定Google帐户的日历数据,以便阅读和创建活动.

不幸的是,我使用OAuth2遇到授权问题.这是我到目前为止所拥有的:

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

2)我在帐户的Google API控制台中启用了Calendar 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
Run Code Online (Sandbox Code Playgroud)

4)我现在想要获得一个AuthToken,以便在与日历通信时使用.我遵循了本教程,但将所有内容转换为适用于Google日历而非Google任务.我通过使用with 成功authTokenAccountManager我想要使​​用的帐户中检索一个.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!
Run Code Online (Sandbox Code Playgroud)

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"
}
Run Code Online (Sandbox Code Playgroud)

7)根据此Google API视频(等待一分钟左右来获取适用的内容),此异常的原因可能是我未在Google API控制台中为该帐户启用API访问.但是,如果你看2),你可以看到我确实这样做了.

8)对我来说,似乎问题是我无法正确设置Simple 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!
Run Code Online (Sandbox Code Playgroud)

10)最后,我的问题是:我想在设备上使用AccountManager中的帐户,以便检索用于Google Calendar API的有效OAuth2令牌.第二种方法对我没用,因为用户必须手动转到他们的网络浏览器并获得授权代码,这不是用户友好的.有人有主意吗?为长篇帖子道歉,谢谢!

小智 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();
Run Code Online (Sandbox Code Playgroud)