2

我有一个 android 应用程序,我必须通过它登录到 facebook。我正在使用 facebook sdk 的会话对象登录。但是,如果设备中安装了实际的 facebook 应用程序,则登录不起作用。

下面是我的代码

public class FacebookSettings extends FragmentActivity{
/** Called when the activity is first created. */

private static final int LOGIN = 0;
private static final int LOGGED_IN = 1;
private static final int FRAGMENT_COUNT = LOGGED_IN +1;
private Button publishButton;
private Fragment[] fragments = new Fragment[FRAGMENT_COUNT];
private Session mSession;

private boolean isResumed = false;


@Override
public void onPause() {
    // TODO Auto-generated method stub
    super.onPause();
    uiHelper.onPause();
    isResumed=false;
}


@Override
public void onResume() {
    // TODO Auto-generated method stub
    super.onResume();
    uiHelper.onResume();
    isResumed=true;
}


protected static final String LOG_TAG = "FACEBOOK_TEST";


@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);


    setContentView(R.layout.fb_settings);
    publishButton=(Button) findViewById(R.id.publishButton);
    android.support.v4.app.FragmentManager fm = getSupportFragmentManager();
    fragments[LOGIN] = fm.findFragmentById(R.id.Login_Fragment);
    fragments[LOGGED_IN] = fm.findFragmentById(R.id.Logged_in_Fragment);

    android.support.v4.app.FragmentTransaction transaction = fm.beginTransaction();
    for(int i = 0; i < fragments.length; i++) {
        transaction.hide(fragments[i]);
    }
    transaction.commit();

    uiHelper = new UiLifecycleHelper(this, callBack);
    uiHelper.onCreate(savedInstanceState);


}

private void showFragment(int fragmentIndex, boolean addToBackStack) {
    android.support.v4.app.FragmentManager fm = getSupportFragmentManager();
    android.support.v4.app.FragmentTransaction transaction = fm.beginTransaction();
    for (int i = 0; i < fragments.length; i++) {
        if (i == fragmentIndex) {
            transaction.show(fragments[i]);
        } else {
            transaction.hide(fragments[i]);
        }
    }
    if (addToBackStack) {
        transaction.addToBackStack(null);
    }
    transaction.commit();
}

private void onSessionStateChange(Session session, SessionState state, Exception exception) {
    // Only make changes if the activity is visible
    if (isResumed) {
        Log.d("facebook","isResumed \n\n\n\n"+state.name());

        android.support.v4.app.FragmentManager manager = getSupportFragmentManager();
        // Get the number of entries in the back stack
        int backStackSize = manager.getBackStackEntryCount();
        // Clear the back stack
        for (int i = 0; i < backStackSize; i++) {
            manager.popBackStack();
        }
        if (state.isOpened()) {
            Log.d("facebook","State isOpened in on session state changed");
            // If the session state is open:
            // Show the authenticated fragment
            publishButton.setVisibility(View.VISIBLE);
            showFragment(LOGGED_IN, false);
        } else if (state.isClosed()) {
            Log.d("facebook","State isClosed in on session state changed");
            // If the session state is closed:
            // Show the login fragment
            publishButton.setVisibility(View.INVISIBLE);
            showFragment(LOGIN, false);
        }
    }
}

@Override
protected void onResumeFragments() {
    // TODO Auto-generated method stub
    super.onResumeFragments();
    Session session = Session.getActiveSession();

    if (session != null && session.isOpened()) {
        // if the session is already open,
        // try to show the selection fragment
        Log.d("facebook","State isOpened in resume fragments\n\n\n");
        publishButton.setVisibility(View.VISIBLE);
        showFragment(LOGGED_IN, false);
    } else {
        // otherwise present the splash screen
        // and ask the user to login.
        Log.d("facebook","State isClosed in resume fragments\n\n\n");
        publishButton.setVisibility(View.INVISIBLE);

        showFragment(LOGIN, false);
    }
}

private UiLifecycleHelper uiHelper;


private Session.StatusCallback callBack=new StatusCallback() {

    @Override
    public void call(Session session, SessionState state, Exception exception) {
        // TODO Auto-generated method stub
        Log.d("facebook","in status call back \n\n\n\n");
        Log.d("facebook","state\n\n\n\n"+session.isClosed());
        Log.d("facebook","state\n\n\n\n"+session.isOpened());
        Log.d("facebook","state\n\n\n\n"+state.isClosed());
        Log.d("facebook","state\n\n\n\n"+state.isOpened());
        onSessionStateChange(session,state,exception);
    }
};


@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    uiHelper.onActivityResult(requestCode, resultCode, data);
}

@Override
public void onDestroy() {
    super.onDestroy();
    uiHelper.onDestroy();
}


@Override
public void onSaveInstanceState(Bundle outState) {
    super.onSaveInstanceState(outState);
    uiHelper.onSaveInstanceState(outState);
}


}

请帮忙谢谢

4

3 回答 3

2

我想你正在寻找这个:

    <meta-data
        android:name="com.facebook.sdk.ApplicationId"
        android:value="@string/applicationId" />

facebook sdk 使用此元数据将会话附加到您的应用程序。您也可以在使用构建器创建会话时设置它

       new Session.Builder(this).setApplicationId(Constants.Facebook.APP_ID)

参考 HelloFacebookSample 中的 AndroidManifest.xml

于 2013-02-07T13:41:04.830 回答
1

我认为问题在于您在 Facebook 页面上输入的密钥哈希与 Facebook 应用程序发送的密钥哈希不同。解决方案之一是在 onCreate() 方法中添加以下代码:

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

try {
    PackageInfo info = getPackageManager().getPackageInfo(
            "com.facebook.samples.loginhowto", 
            PackageManager.GET_SIGNATURES);
    for (Signature signature : info.signatures) {
        MessageDigest md = MessageDigest.getInstance("SHA");
        md.update(signature.toByteArray());
        Log.d("KeyHash:", Base64.encodeToString(md.digest(), Base64.DEFAULT));
        }
} catch (NameNotFoundException e) {

} catch (NoSuchAlgorithmException e) {

}
...

将 com.facebook.samples.loginhowto 替换为您的包裹信息。在日志中查找 KeyHash 并将其添加到您的开发人员页面中。您可以在 Facebook 开发者页面facebook上找到更多信息。但请记住,这是临时解决方案,很可能仅适用于您从 Facebook 应用程序获得密钥哈希的帐户。

于 2013-06-20T14:43:47.817 回答
0

我们也遇到过同样的问题,但它是在 iPhone 应用程序中。当我们在 iPhone 上开发一个 facbook 应用程序时,如果安装了 fb 应用程序本身,login 不起作用。我们解析它,将我在其中创建 facebook 应用程序的 Iphone 应用程序包标识符放在 facebook 开发人员页面中。

我希望当您在 db 开发人员页面中为 android 创建一个 fb 应用程序时,这种类似的字段会丢失。请检查一下。

于 2013-02-07T11:04:43.617 回答