AndroidでDropBoxの認証を行う。
こちらを参考にDropBox APIの認証を行うサンプルを作りました。
- Dropbox for Developersにアクセスしてログイン。
- My Appsの[Create an App]を押し、アプリケーション名と内容を入力して[Create]ボタンを押す。
- Dropbox APIを使用するためには、次の画面に表示されるApp key と App secret が必要です。
- Android向けDropbox SDKのダウンロード
- Development kitsよりAndroidを選択してzipファイルをダウンロードする。
- zipファイルを解凍する。(好きなところに移動する)
- Androidプロジェクトを作成する
- 作成したプロジェクトに先程ダウンロードしたDropBox SDKのライブラリーを追加する。
- プロジェクト右クリック
- Build Path→Configure Build Path→Librariesタグ→Add external JARs...を選択
- 解凍したSDKのフォルダ→lib→フォルダ内にあるjarを全て追加してOKボタンを押す。(下記の3つ)]
- AndroidManifest.xmlを修正する。
1.認証用に以下のコードをapplicationタグ内に貼りつけて、のdb-の後ろをApp keyで置き換える。
<activity android:name="com.dropbox.client2.android.AuthActivity" android:launchMode="singleTask" android:configChanges="orientation|keyboard"> <intent-filter> <!-- Change this to be db- followed by your app key --> <data android:scheme="db-INSERT-APP-KEY-HERE" /> <action android:name="android.intent.action.VIEW" /> <category android:name="android.intent.category.BROWSABLE"/> <category android:name="android.intent.category.DEFAULT" /> </intent-filter> </activity>
2.
サンプルプロジェクトのAndroidManifest.xml全文はこちら
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.sakurafish.example.dropboxauth" android:versionCode="1" android:versionName="1.0" > <uses-sdk android:minSdkVersion="7" /> <!-- INTERNET接続のパーミッションが必要 --> <uses-permission android:name="android.permission.INTERNET" /> <application android:icon="@drawable/ic_launcher" android:label="@string/app_name" > <activity android:label="@string/app_name" android:name=".DropBoxAuthActivity" > <intent-filter > <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> <activity android:configChanges="orientation|keyboard" android:launchMode="singleTask" android:name="com.dropbox.client2.android.AuthActivity" > <intent-filter > <!-- db- の後ろをあなたのアプリケーションの app key に置き換えて下さい --> <data android:scheme="db-ここを変えてね" /> <action android:name="android.intent.action.VIEW" /> <category android:name="android.intent.category.BROWSABLE" /> <category android:name="android.intent.category.DEFAULT" /> </intent-filter> </activity> </application> </manifest>
8.画面のレイアウトを作成する。(layout/main.xml)
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" > <Button android:id="@+id/auth_button" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="Link with Dropbox" android:onClick="onClick_auth" /> </LinearLayout>
9.ソースを編集する。ソース全文はこちら。Dropbox SDKに入っていたサンプルを必要なところだけコピペして作ったので英語と日本語が混在してます。すみません。App key と App secretをプリファレンスに保存しているところもサンプルそのままです。
package com.sakurafish.example.dropboxauth; import com.dropbox.client2.DropboxAPI; import com.dropbox.client2.android.AndroidAuthSession; import com.dropbox.client2.session.AccessTokenPair; import com.dropbox.client2.session.AppKeyPair; import com.dropbox.client2.session.Session.AccessType; import android.app.Activity; import android.content.SharedPreferences; import android.content.SharedPreferences.Editor; import android.os.Bundle; import android.view.View; /** * DropBox認証 * * @author sakura * */ public class DropBoxAuthActivity extends Activity { /** ここを自分のアプリのAPP_KEYとAPP_SECRETに変更する */ final static private String APP_KEY = "ここを変えてね"; final static private String APP_SECRET = "ここを変えてね"; /** アクセスタイプをfullにする時はここを変更する */ final static private AccessType ACCESS_TYPE = AccessType.APP_FOLDER; /** ここは変更しなくていい */ final static private String ACCOUNT_PREFS_NAME = "prefs"; final static private String ACCESS_KEY_NAME = "ACCESS_KEY"; final static private String ACCESS_SECRET_NAME = "ACCESS_SECRET"; DropboxAPI< AndroidAuthSession > mApi; private boolean mLoggedIn; /** Called when the activity is first created. */ @Override public void onCreate( Bundle savedInstanceState ) { super.onCreate( savedInstanceState ); setContentView( R.layout.main ); // We create a new AuthSession so that we can use the Dropbox API. AndroidAuthSession session = buildSession(); mApi = new DropboxAPI< AndroidAuthSession >( session ); } /** * ボタンがクリックされた時の処理 */ public void onClick_auth( View view ) { // This logs you out if you're logged in, or vice versa if ( mLoggedIn ) { logOut(); } else { // Start the remote authentication mApi.getSession().startAuthentication( DropBoxAuthActivity.this ); } } private void logOut() { // Remove credentials from the session mApi.getSession().unlink(); // Clear our stored keys clearKeys(); } private AndroidAuthSession buildSession() { AppKeyPair appKeyPair = new AppKeyPair( APP_KEY, APP_SECRET ); AndroidAuthSession session; String[] stored = getKeys(); if ( stored != null ) { AccessTokenPair accessToken = new AccessTokenPair( stored[ 0 ], stored[ 1 ] ); session = new AndroidAuthSession( appKeyPair, ACCESS_TYPE, accessToken ); } else { session = new AndroidAuthSession( appKeyPair, ACCESS_TYPE ); } return session; } /** * Shows keeping the access keys returned from Trusted Authenticator in a local store, rather than storing user name & password, and re-authenticating each time (which is not to be done, ever). * * @return Array of [access_key, access_secret], or null if none stored */ private String[] getKeys() { SharedPreferences prefs = getSharedPreferences( ACCOUNT_PREFS_NAME, 0 ); String key = prefs.getString( ACCESS_KEY_NAME, null ); String secret = prefs.getString( ACCESS_SECRET_NAME, null ); if ( key != null && secret != null ) { String[] ret = new String[ 2 ]; ret[ 0 ] = key; ret[ 1 ] = secret; return ret; } else { return null; } } private void clearKeys() { SharedPreferences prefs = getSharedPreferences( ACCOUNT_PREFS_NAME, 0 ); Editor edit = prefs.edit(); edit.clear(); edit.commit(); } }
10.実行すると次のようになります