すでにアプリのログイン手順がありましたが、Facebook グラフ API と統合しようとすると、FB でログインしようとすると、アプリが受け入れられません。ここで FB ボタンのコードを貼り付けています。
case R.id.fbButton:
authenticateOnFacebook();
sendFacebookIdToServer(APP_ID, getApplicationContext());
break;
Facebookの認証方法、これで私はgetcredentials方法も使用しています。
public void authenticateOnFacebook()
{
try{
String[] permissions={"user_birthday", "email"};
facebook.authorize(this.activity, permissions, Facebook.FORCE_DIALOG_AUTH, new DialogListener() {
@Override
public void onFacebookError(FacebookError e) {
}
@Override
public void onError(DialogError e) {
}
@Override
public void onComplete(Bundle values) {
try{
String facebookId = getCredentials((String) values.get("access_token"));
sendFacebookIdToServer(facebookId, context);
} catch(Exception e){
e.printStackTrace();
}
}
@Override
public void onCancel() {
}
});
} catch(Exception e){
e.printStackTrace();
}
}
protected String getCredentials(String token) {
JSONObject jObject=null;
String facebookId = null;
Bundle params = new Bundle();
params.putString(Facebook.TOKEN, token);
try {
jObject = new JSONObject(facebook.request("me", params));
}catch(Exception e){
e.printStackTrace();
}
if(jObject!=null)
{
try {
facebookId = jObject.getString("id");
}catch(Exception e){
e.printStackTrace();
}
}
return facebookId;
}
サーバーにリクエストを送信して認証するために、このメソッドを使用しています
public void sendFacebookIdToServer(String facebookId, Context context){
String accessToken = null;
DatabaseHelper helper = new DatabaseHelper(context);
DatabaseUtility dao = new DatabaseUtility(helper);
try {
accessToken = dao.getAccessToken();
} catch (Exception e1) {
e1.printStackTrace();
}
Map<String , String> params = new HashMap<String,String>();
params.put(Constants.FACEBOOK_ID, facebookId);
params.put(Constants.ACCESS_TOKEN_PARAM, accessToken);
Status status = null;
try {
status = Utils.addFacebookAccount(params, context);
} catch (NullPointerException e) {
e.printStackTrace();
} catch (JSONException e) {
e.printStackTrace();
}
if((status != null) && (status.getStatus().equalsIgnoreCase(Constants.SUCCESS))){
navigateNowLisnScreenWithSuccess();
}
}
private void navigateNowLisnScreenWithSuccess() {
// TODO Auto-generated method stub
Intent i = new Intent(context,TabView.class);
startActivity(i);
}
ログキャット
01-16 21:40:26.098: E/Cursor(1299): at android.view.View.performClick(View.java:2408)
01-16 21:40:26.108: E/Cursor(1299): Finalizing a Cursor that has not been deactivated or closed. database = /data/data/com.lisnx.activity/databases/lisnx.db, table = access_token, query = SELECT token FROM access_token
01-16 21:40:26.108: E/Cursor(1299): android.database.sqlite.DatabaseObjectNotClosedException: Application did not close the cursor or database object that was opened here
01-16 21:40:26.108: E/Cursor(1299): at android.database.sqlite.SQLiteCursor.<init>(SQLiteCursor.java:210)
01-16 21:40:26.108: E/Cursor(1299): at android.database.sqlite.SQLiteDirectCursorDriver.query(SQLiteDirectCursorDriver.java:53)
01-16 21:40:26.108: E/Cursor(1299): at android.database.sqlite.SQLiteDatabase.rawQueryWithFactory(SQLiteDatabase.java:1345)
01-16 21:40:26.108: E/Cursor(1299): at android.database.sqlite.SQLiteDatabase.queryWithFactory(SQLiteDatabase.java:1229)
01-16 21:40:26.108: E/Cursor(1299): at com.lisnx.activity.LoginActivity.sendFacebookIdToServer(LoginActivity.java:223)
01-16 21:40:26.108: E/Cursor(1299): at com.lisnx.activity.LoginActivity.onClick(LoginActivity.java:263)
01-16 21:40:26.108: E/Cursor(1299): at android.view.View.performClick(View.java:2408)
01-16 21:40:26.108: E/Cursor(1299): at android.view.View$PerformClick.run(View.java:8816)
01-16 21:40:26.108: E/Cursor(1299): at android.os.Handler.handleCallback(Handler.java:587)
01-16 21:40:26.108: E/Cursor(1299): at android.os.Handler.dispatchMessage(Handler.java:92)
01-16 21:40:26.108: E/Cursor(1299): at android.os.Looper.loop(Looper.java:123)
01-16 21:40:26.108: E/Cursor(1299): at android.app.ActivityThread.main(ActivityThread.java:4627)
01-16 21:40:26.108: E/Cursor(1299): at java.lang.reflect.Method.invokeNative(Native Method)
01-16 21:40:26.108: E/Cursor(1299): at java.lang.reflect.Method.invoke(Method.java:521)
01-16 21:40:26.108: E/Cursor(1299): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
01-16 21:40:26.108: E/Cursor(1299): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
01-16 21:40:26.108: E/Cursor(1299): at dalvik.system.NativeStart.main(Native Method)