Feed Dialogのmessageプロパティが無効になっていたので、FacebookのDialogを表示して、そのEditTextに投稿したい文章をデフォルト表示することができなくなってしまいました。
なので、Facebookのウォール投稿用のActivityを作って、そこにウォールに投稿したい文章をデフォルト表示するように変更しようと思い、やりかたを調べていたところ、Facebook SDK for Androidが2011年10月11日に更新されているのに気が付きました。
facebook/facebook-android-sdk – GitHub
早速こちらのSDKを使って試してみたいと思います。
画面は、gTranslatorにFacebookのウォールに投稿する機能を追加してみた(実装編)のサンプルと同様のものにしました。
「post to facebook」ボタンを押すと、EditTextに入力された文章をFacebookのウォールに投稿するというものです。
Activityのレイアウトは以下のようになっています。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 | <? xml version = "1.0" encoding = "utf-8" ?> android:orientation = "vertical" android:layout_width = "fill_parent" android:layout_height = "fill_parent" > < EditText android:id = "@+id/text" android:layout_width = "fill_parent" android:layout_height = "fill_parent" android:layout_weight = "1" android:layout_margin = "5dp" android:inputType = "textMultiLine" android:gravity = "top" /> < Button android:id = "@+id/post" android:text = "post to facebook" android:layout_width = "fill_parent" android:layout_height = "wrap_content" /> </ LinearLayout > |
Activityのソースコードです。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 | package asia.live_cast.translator.sample; import java.io.FileNotFoundException; import java.io.IOException; import java.net.MalformedURLException; import com.facebook.android.AsyncFacebookRunner; import com.facebook.android.AsyncFacebookRunner.RequestListener; import com.facebook.android.DialogError; import com.facebook.android.Facebook; import com.facebook.android.Facebook.DialogListener; import com.facebook.android.FacebookError; import android.app.Activity; import android.content.Intent; import android.os.Bundle; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.EditText; public class TranslatorSample16Activity extends Activity { Facebook facebook = new Facebook( "xxxxxxxxxxxxxxx" ); AsyncFacebookRunner runner = new AsyncFacebookRunner(facebook); @Override public void onCreate(Bundle savedInstanceState) { super .onCreate(savedInstanceState); setContentView(R.layout.main); final EditText text = (EditText)findViewById(R.id.text); Button btnPost = (Button)findViewById(R.id.post); btnPost.setOnClickListener( new OnClickListener() { public void onClick(View v) { facebook.authorize(TranslatorSample16Activity. this , new String[] { "publish_stream" }, new DialogListener() { public void onComplete(Bundle values) { Bundle bundle = new Bundle(); bundle.putString( "message" , text.getText().toString()); runner.request( "me/feed" , bundle, "POST" , new RequestListener() { public void onComplete(String response, Object state) { } public void onIOException(IOException e, Object state) { } public void onFileNotFoundException( FileNotFoundException e, Object state) { } public void onMalformedURLException( MalformedURLException e, Object state) { } public void onFacebookError(FacebookError e, Object state) { } }, null ); } public void onFacebookError(FacebookError e) { } public void onError(DialogError e) { } public void onCancel() { } }); } }); } @Override public void onActivityResult( int requestCode, int resultCode, Intent data) { super .onActivityResult(requestCode, resultCode, data); facebook.authorizeCallback(requestCode, resultCode, data); } } |
ボタンクリック時に文章を投稿する部分の処理は37〜42行目になります。
AsyncFacebookRunnerクラスを使います。
1 2 | Facebook facebook = new Facebook( "xxxxxxxxxxxxxxx" ); AsyncFacebookRunner runner = new AsyncFacebookRunner(facebook); |
authorizeメソッドで認証し、
1 | facebook.authorize(TranslatorSample16Activity. this , new String[] { "publish_stream" }, new DialogListener() { |
認証が通ったら、AsyncFacebookRunnerクラスのrequestメソッドで投稿したい文章をpostします。
1 2 3 | Bundle bundle = new Bundle(); bundle.putString( "message" , text.getText().toString()); runner.request( "me/feed" , bundle, "POST" , new RequestListener() { |
emulatorで実行してみたら、以下のようなエラーが発生しました。
invalid_key:Android key mismatch. Your key “xxxxxxxxxxxxxxxxxxxxxxxxxxx” does not match the allowed keys specified in your application settings. Check your application settings at http://www.facebook.com/developers
キーハッシュが不正なようです。
キーハッシュは何も変更していないので不思議だったのですが、ちょっと調べてみたところ、どうやらDebug環境では、Debug用のキーハッシュを使わないといけないようです。
android – login failed invalid key error with facebook sdk – Stack Overflow
※ 以前は、特に問題なくpostできてましたけど・・・
ということで、あらためてキーハッシュを作り直し、
keytool -exportcert -alias androiddebugkey -keystore ~/.android/debug.keystore | openssl sha1 -binary | openssl base64
テスト用のFacebookアプリを登録して、再度実行してみたところ、今度は正常にpostできるようになりました。
※ Facebookアプリ登録の方法は、gTranslatorにFacebookのウォールに投稿する機能を追加してみた(Facebook登録編)を参考にしていただければと思います。
サンプルソース
- TranslatorSample16:ダウンロード