简体   繁体   English

如何将facebook登录集成到Android应用程序中

[英]How to integrate facebook login into android app

I am trying to integrate facebook login into my app 我正在尝试将facebook登录集成到我的应用程序中

I went over this tutorial : https://developers.facebook.com/docs/android/getting-started/facebook-sdk-for-android/ 我浏览了这个教程: https//developers.facebook.com/docs/android/getting-started/facebook-sdk-for-android/

downloaded facebook sdk 3.5 下载了facebook sdk 3.5

step by step - downloaded openssl , created an androidkeystore , generated hashkey, created an app in facebook development console, gave it my package name, and the activity that logs in, and the hashcode printed to me by the device in the log console, as the tutorial suggested + the hashkey i generated with openssl, added the app_id to the strings file and the required permissions activities and metadata to the android manifest file 一步一步 - 下载openssl,创建一个androidkeystore,生成hashkey,在facebook开发控制台中创建一个应用程序,给它我的包名,登录的活动,以及设备在日志控制台中打印给我的哈希码,如教程建议+我用openssl生成的hashkey,将app_id添加到strings文件中,并将所需的权限活动和元数据添加到android清单文件中

now i opened the app and clicked the "login with facebook button" it asked for my permission to user profile, i clicked ok 现在我打开了应用程序并单击“使用facebook登录按钮”它要求我允许用户个人资料,我点击确定

and then the log printed this exception : 然后日志打印了这个异常:

10-16 19:51:20.718: W/Bundle(8444): Key com.facebook.platform.protocol.PROTOCOL_VERSION expected String but value was a java.lang.Integer.  The default value <null> was returned.
10-16 19:51:20.718: W/Bundle(8444): Attempt to cast generated internal exception:
10-16 19:51:20.718: W/Bundle(8444): java.lang.ClassCastException: java.lang.Integer
10-16 19:51:20.718: W/Bundle(8444):     at android.os.Bundle.getString(Bundle.java:1040)
10-16 19:51:20.718: W/Bundle(8444):     at android.content.Intent.getStringExtra(Intent.java:3412)
10-16 19:51:20.718: W/Bundle(8444):     at com.facebook.AuthorizationClient$KatanaLoginDialogAuthHandler.tryAuthorize(AuthorizationClient.java:829)
10-16 19:51:20.718: W/Bundle(8444):     at com.facebook.AuthorizationClient.tryCurrentHandler(AuthorizationClient.java:278)
10-16 19:51:20.718: W/Bundle(8444):     at com.facebook.AuthorizationClient.tryNextHandler(AuthorizationClient.java:244)
10-16 19:51:20.718: W/Bundle(8444):     at com.facebook.AuthorizationClient$GetTokenAuthHandler.getTokenCompleted(AuthorizationClient.java:778)
10-16 19:51:20.718: W/Bundle(8444):     at com.facebook.AuthorizationClient$GetTokenAuthHandler$1.completed(AuthorizationClient.java:737)
10-16 19:51:20.718: W/Bundle(8444):     at com.facebook.internal.PlatformServiceClient.callback(PlatformServiceClient.java:144)
10-16 19:51:20.718: W/Bundle(8444):     at com.facebook.internal.PlatformServiceClient.handleMessage(PlatformServiceClient.java:128)
10-16 19:51:20.718: W/Bundle(8444):     at com.facebook.internal.PlatformServiceClient$1.handleMessage(PlatformServiceClient.java:54)
10-16 19:51:20.718: W/Bundle(8444):     at android.os.Handler.dispatchMessage(Handler.java:99)
10-16 19:51:20.718: W/Bundle(8444):     at android.os.Looper.loop(Looper.java:130)
10-16 19:51:20.718: W/Bundle(8444):     at android.app.ActivityThread.main(ActivityThread.java:3906)
10-16 19:51:20.718: W/Bundle(8444):     at java.lang.reflect.Method.invokeNative(Native Method)
10-16 19:51:20.718: W/Bundle(8444):     at java.lang.reflect.Method.invoke(Method.java:507)
10-16 19:51:20.718: W/Bundle(8444):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:840)
10-16 19:51:20.718: W/Bundle(8444):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:598)
10-16 19:51:20.718: W/Bundle(8444):     at dalvik.system.NativeStart.main(Native Method)

which is a warning, the app didn't crash because of it, but the login itself fails 这是一个警告,应用程序没有崩溃,因为它,但登录本身失败

this is my code for the login flow: 这是我的登录流程代码:

 import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;

import com.facebook.Session;
import com.facebook.SessionState;
import com.facebook.UiLifecycleHelper;

public class MainActivity extends FragmentActivity {

private static final int SPLASH = 0;
private static final int SELECTION = 1;
private static final int FRAGMENT_COUNT = SELECTION + 1;

private Fragment[] fragments = new Fragment[FRAGMENT_COUNT];

private boolean isResumed = false;

private UiLifecycleHelper uiHelper;
private Session.StatusCallback callback = 
    new Session.StatusCallback() {
    @Override
    public void call(Session session, 
            SessionState state, Exception exception) {
        onSessionStateChange(session, state, exception);
    }
};
/**
 * @param savedInstanceState
 */
@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    uiHelper = new UiLifecycleHelper(this, callback);
    uiHelper.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    FragmentManager fm = getSupportFragmentManager();
    fragments[SPLASH] = fm.findFragmentById(R.id.splashFragment);
    fragments[SELECTION] = fm.findFragmentById(R.id.selectionFragment);

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


private void showFragment(int fragmentIndex, boolean addToBackStack) {
    FragmentManager fm = getSupportFragmentManager();
    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();
}

@Override
public void onResume() {
    super.onResume();
    uiHelper.onResume();
    isResumed = true;
}

@Override
public void onPause() {
    super.onPause();
    uiHelper.onPause();
    isResumed = false;
}

@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
protected void onSaveInstanceState(Bundle outState) {
    super.onSaveInstanceState(outState);
    uiHelper.onSaveInstanceState(outState);
}

private void onSessionStateChange(Session session, SessionState state, Exception exception) {
    // Only make changes if the activity is visible
    if (isResumed) {
        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()) {
            // If the session state is open:
            // Show the authenticated fragment
            showFragment(SELECTION, false);
        } else if (state.isClosed()) {
            // If the session state is closed:
            // Show the login fragment
            showFragment(SPLASH, false);
        }
    }
}

@Override
protected void onResumeFragments() {
    super.onResumeFragments();
    Session session = Session.getActiveSession();

    if (session != null && session.isOpened()) {
        // if the session is already open,
        // try to show the selection fragment
        showFragment(SELECTION, false);
    } else {
        // otherwise present the splash screen
        // and ask the person to login.
        showFragment(SPLASH, false);
    }
}

} }

What am i doing wrong? 我究竟做错了什么? why doesnt it work ? 为什么它不起作用?

The problem was - 问题是 -

nowhere on the tutorial page did it say that i MUST exit the sandbox mode if i am trying to login with an account that is not the admin of the facebook app or one of the defined fake accounts 如果我试图使用不是facebook应用程序的管理员或其中一个已定义的假帐户登录,我必须在教程页面上说它必须退出沙盒模式

once i put the app into live mode, it worked 一旦我将应用程序置于实时模式,它就有效

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM