私は、Google カレンダー API と通信する必要がある Android Honeycomb (v3.0) アプリケーションに取り組んでいます。イベントの読み取りと作成のために、アプリケーションが特定の 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
4) カレンダーと通信するときに使用する AuthToken を取得したいと考えています。このチュートリアルに従いましたが、Google タスクではなく Google カレンダーで動作するようにすべてを変換しました。withを使用して、使用したいアカウントでauthToken
からを正常に取得しました。AccountManager
getAuthToken
AUTH_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 ビデオ(該当するコンテンツに到達するまで 1 分ほど待ちます) によると、この例外の理由は、アカウントの Google API コンソール内で API アクセスを有効にしていないことが原因である可能性があります。しかし、2) を見ると、私がそうしたことがわかります。
Calendar.setKey
8)メソッドが廃止されたため、Simple API Access Key を正しく設定できなかったことが問題のようです。以前にリンクした 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) 最後に、私の質問: Google カレンダー API で使用する OAuth2 トークンを取得するために、デバイス自体の AccountManager からアカウントを使用したいと考えています。2 番目の方法は、ユーザーが手動で Web ブラウザーにアクセスして認証コードを取得する必要があり、ユーザー フレンドリーではないため、私には役に立ちません。誰にもアイデアはありますか?長文失礼しました、ありがとうございます!