Superdry Memorandom :-p

旧「superdry memorandum :-D」です

tab APIを使ったAndroidサンプルアプリ(1)

今回はAndroid用のサンプルアプリケーションをご紹介します。開発作業の基本的な流れはWEBやiPhoneアプリケーションと変わりませんので、tab APIの紹介エントリも合わせて見てください。

STEP1 アプリケーションを登録してクライアントIDを取る

基本的には、iOSの場合とほぼ同じようにtabの開発者用サイトから入手してください。

STEP2 アプリケーションを開発する

大まかな流れはREADMEに書いてあるのでそちらを見てください。

開発環境を準備する

まずは、Androidの開発環境を準備してください。開発環境の準備方法は、Androidの公式サイトを参照してください。

また、Android用のサンプルアプリケーションGithubで公開しています。これをダウンロードし、Eclipseでプロジェクトフォルダをインポートしてください。具体的には、Eclipseのメニューバーより、[File] - [New] - [Other] - [Android Project from Existing Code] を選択し、

<<ダウンロードフォルダ>>/oauth/android/SampleApp

を指定します。

f:id:Superdry:20191111095554p:plain

最後に、OAuthのライブラリをダウンロードします。このサンプルアプリケーションでは、leelooのライブラリを使用します。

leelooのサイトからOAuthのライブラリをダウンロードします。今回使用するjarファイルは以下の4つになります。

  • jettison-1.2.jar

  • oauth2-client.jar

  • oauth2-common-0.1.jar

  • slf4j-api.1.6.1.jar

この4つのjarファイルを入手したら、libsフォルダの下ににこれらjarファイルをコピーします。

サンプルアプリケーションのカスタマイズ

まずは、CommonConst.javaに、入手したクライアントID等を埋め込みます。

package com.tonchidot.tab.sample.oauth;

import android.os.Build;

public final class CommonConst {

    public static final String TAG = "tabsamle";

    public static final String PREF_NAME = "tab";
    public static final String PREF_ACCESS_TOKEN = "access_token";
    public static final String PREF_REFRESH_TOKEN = "refresh_token";
    public static final String PREF_EXPIRES_IN = "expires_in";
    public static final String PREF_TOKEN_TYPE = "token_type";

    public final static String AUTHORIZATION_LOCATION = "https://tab.do/oauth2/authorize";
    public final static String TOKEN_LOCATION = "https://tab.do/api/1/oauth2/token";
    public static final String ACCESS_URI = "https://tab.do/api/1/users/me.json";

    public final static String CLIENT_ID = "<<Your Client ID>>";
    public final static String CLIENT_SECRET = "<<Your Client Secret>>";
    public final static String REDIRECT_URI = "<<Your Redirect Uri>>";

    public static boolean isHoneycomb() {
        return Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB;
    }

}

L19〜L21にそれぞれ入手したクライアントID、クライアントシークレット、コールバックURLに置き換えます。コールバックURLはデフォルトのままだと"tab://callback/oauth2"になります。もしここで独自のURIスキーマを設定した場合は、AndroidManifest.xmlも修正します。

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.tonchidot.tab.sample.oauth"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="10"
        android:targetSdkVersion="15" />

    <uses-permission android:name="android.permission.INTERNET" />

    <application
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name=".MainActivity"
            android:label="@string/title_activity_main"
            android:screenOrientation="portrait" >
            <meta-data
                android:name="android.support.PARENT_ACTIVITY"
                android:value="android.support.v4.app.FragmentActivity" />

            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity
            android:name=".GetAccessTokenActivity"
            android:exported="true"
            android:label="@string/title_activity_get_accesstoken"
            android:screenOrientation="portrait" >
            <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:host="callback"
                    android:scheme="tab" />
            </intent-filter>
        </activity>
    </application>

</manifest>

L43行目のIntent filterのandroid:schemaの設定を変更します。

これでアプリケーションのカスタマイズは終了です。ビルドののちエミュレータまたは実機で起動してください。

STEP3 アプリケーションを起動する

アプリケーションを起動すると、OAuth認証というボタンが表示されます。

f:id:Superdry:20191111100428p:plain

ボタンをクリックすると、ブラウザでtabの認可画面が開きます。

f:id:Superdry:20191111100442p:plain

「許可する」をタップします。すると、アプリに戻り、認証コードの取得、アクセストークンの取得処理が始まり、認証後プロフィール情報が取得できます。(このサンプルアプリケーションではログインしているユーザ自身の情報を取得します。)

f:id:Superdry:20191111100456p:plain

次回は、サンプルアプリケーションの認証の実装部分についてご紹介します。