簡単に言うと、Android アプリケーションに OAuth フローを実装する方法については、あなたが正しいということです。大まかに言えば、アプリケーションは次のことを行います。
- InitializeOAuth Choreo を実行する
- InitializeOAuth によって返された認証 URL を指す WebView を開きます
- ユーザーが WebView で [許可] をクリックした後、FinalizeOAuth Choreo を実行してアクセス トークンを取得します。
上記の #3 の秘訣は、「インテント フィルター」を使用して、カスタム URL 処理スキームを Android に登録する機能です。AndroidManifest.xml ファイルで、次のようなコードを使用して、アクティビティの 1 つにカスタム インテント フィルターを割り当てます。
<activity android:name=".MyOAuthActivity">
<intent-filter>
<action android:name = "android.intent.action.VIEW" />
<category android:name="android.intent.category.DEFAULT" />
<category android:name="android.intent.category.BROWSABLE" />
<data android:scheme="temboo" android:host="twitter" />
</intent-filter>
</activity>
このコードが意味することは、アプリケーションが「temboo://twitter」のような URL のリクエストを受信した場合、そのリクエストは指定したアクティビティ (この場合は MyOAuthActivity) に自動的に転送されるということです。
InitializeOAuth Choreo を実行するときは、「tempboo://twitter」(または使用する任意のカスタム インテント スキーム) を「転送 URL」入力として指定する必要があります。これにより、ユーザーが OAuth Web ビューで [許可] をクリックした後、Temboo はリクエストをアクティビティに転送します。
アクティビティでは、次のようなコードを使用して、カスタム スキームを使用して URL をインターセプトできます。
// Find the webview, and make sure Javascript is enabled.
WebView webview = (WebView)findViewById(R.id.oauthWebview);
webview.getSettings().setJavaScriptEnabled(true);
webview.setWebViewClient(new WebViewClient() {
// Here we override the onPageStarted method of the webview. If Twitter authorization
// succeeds, we'll be redirected to a URL that looks like temboo://twitter
public void onPageStarted(WebView view, String url, Bitmap favicon) {
if(url.startsWith("temboo://")) {
handled = true;
// We got forwarded here from the 3rd party OAuth approval page; proceed
// to next step
Log.i("Temboo", "Got callback!");
Intent i = new Intent(getBaseContext(), FinalizeOAuthActivity.class);
i.putExtra("callbackID", callbackID);
startActivity(i);
}
}
});
webview.loadUrl(authorizationURL);`
ところで、私は Temboo で働いています。ご不明な点がございましたら、お気軽にお問い合わせください。