
サンプルはJSPでOAuthを分かりやすく記述していますが
手数が多く記述してあるので、実装としてはわかりずらいので
一旦整理してみましょう。
Google for Eclipse Plugin[更新サイト:http://dl.google.com/eclipse/plugin/3.6] と
Slim3 Plugin[更新サイト:http://slim3.googlecode.com/svn/updates/]をインストールします。
Slim3はGoogleAppEngineに最適化されたMVCフレームワークです。
非常に簡単なので説明は省きます。
indexを作成して
- SimpleOAuthRequest oauthRequestor =
- new SimpleOAuthRequest(requestTokenUrl,consumerKey,consumerSecret,null);
- //現在のURLからコールバックURLを作成
- String thisUrl = request.getRequestURL().toString();
- String cbUrl = thisUrl.substring(0, thisUrl.lastIndexOf('/') + 1);
- cbUrl = cbUrl + "callback";
- oauthRequestor.setParameter("oauth_callback", cbUrl);
- Map<string, string=""> reply = oauthRequestor.sendRequest();
- String requestToken = reply.get("oauth_token");
- String authorizationUrl = authorizationUrlBase + "?oauth_token=" + requestToken;
- //認証にリダイレクトを行う
- return redirect(authorizationUrl);
- </string,>
設定値などはコールバックURL以外はそのままです。
SimpleOAuthRequestを利用して認証用のURLを作成してそのままリダイレクトをかけてます。
※もちろんログインボタンとか作りたいなら別ですけど。
これにより/indexにアクセスするとEvernoteの認可画面が表示されるはずです。
それではコールバックURLを指定してみましょう。
callbackを作成して
- SimpleOAuthRequest oauthRequestor = new SimpleOAuthRequest( requestTokenUrl, consumerKey, consumerSecret, null);
- String requestToken = requestScope("oauth_token");
- String verifier = requestScope("oauth_verifier");
- oauthRequestor.setParameter( "oauth_token", requestToken);
- oauthRequestor.setParameter( "oauth_verifier", verifier);
- Map<string, string=""> reply = oauthRequestor.sendRequest();
- String accessToken = reply.get("oauth_token");
- String shardId = reply.get("edam_shard");
- String noteStoreUrl = noteStoreUrlBase + shardId;
- THttpClient noteStoreTrans = new THttpClient(noteStoreUrl);
- TBinaryProtocol noteStoreProt = new TBinaryProtocol(noteStoreTrans);
- NoteStore.Client noteStore =
- new NoteStore.Client(noteStoreProt, noteStoreProt);
- List<notebook> notebooks = noteStore.listNotebooks(accessToken);
- for (Notebook notebook : notebooks) {
- logger.info("Notebook: " + notebook.getName());
- }
- ook></string,>
これもサンプルと同じ実装ですね。
もちろん認証してなかったら~とかの処理は必要ですが、
何やっているかを分かりやすくするため、2つのURLだけで実装してみました。
ListのところはNotebookを返してたので?じゃなくしました。
THttpClient はライブラリに存在するthriftのAPIですね。
それらを利用してEvernoteのAPIを利用するイメージです。
SimpleOAuthRequestの実装を説明しないと何やってるかわかりずらいですね。
次回はSimpleOAuthRequestについて記述してみましょう。
0 件のコメント:
コメントを投稿