Facebook SDK 3.5:获取用户数据示例在模拟器中工作,但在实际设备中不起作用

时间:2013-12-09 08:14:52

标签: android facebook facebook-android-sdk facebook-login

我正在尝试在developers.facebook.com上实现fetch用户数据示例 它在Emulator上运行正常,并在logcat中显示数据 但是当我在真实设备中尝试它时,它不会打开登录屏幕 这就是为什么它直接将消息写入Logcat“Logged out”(我猜这意味着状态已关闭) 以下是代码

private void onSessionStateChange(会话会话,SessionState状态,异常异常){

if (state.isOpened()) {
           Log.i(TAG, "Logged in...");
        userInfoTextView.setVisibility(View.VISIBLE);

        Request.newMeRequest(session, new Request.GraphUserCallback() {

              // callback after Graph API response with user object
          @Override
          public void onCompleted(GraphUser user, Response response) {

      if (user != null) {
                // Display the parsed user info
                buildUserInfoDisplay(user);
                Log.d(TAG,"Data Displayed");
            }
          }
        }).executeAsync();

    } else if (state.isClosed()) {
        Log.i(TAG, "Logged out...");
        userInfoTextView.setVisibility(View.INVISIBLE);
    }
}

@Override
public View onCreateView(LayoutInflater inflater, 
        ViewGroup container, 
        Bundle savedInstanceState) {
    View view = inflater.inflate(R.layout.fblogin, container, false);
    LoginButton authButton = (LoginButton) view.findViewById(R.id.authButton);
    authButton.setFragment(this);
    authButton.setReadPermissions(Arrays.asList("user_likes", "user_status","user_location","email","user_birthday"));
    Log.d(TAG,"onCreateview");

    userInfoTextView = (TextView) view.findViewById(R.id.userInfoTextView);

    return view;

}

private Session.StatusCallback callback = new Session.StatusCallback() {
    @Override
    public void call(Session session, SessionState state, Exception exception) {
        onSessionStateChange(session, state, exception);
    }
};

@Override
public void onResume() {
    super.onResume();
    // For scenarios where the main activity is launched and user
    // session is not null, the session state change notification
    // may not be triggered. Trigger it if it's open/closed.
    Session session = Session.getActiveSession();
    if (session != null &&
           (session.isOpened() || session.isClosed()) ) {
        onSessionStateChange(session, session.getState(), null);
    }

    uiHelper.onResume();
}

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

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

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

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

private void buildUserInfoDisplay(GraphUser user) {
    /*if(user.getLocation()!=null){
    userInfo.append(String.format("ID: %s\n\n", 
            user.getLocation()));
    }*/
    //Log.d(TAG+" Location",user.getLocation().toString());

    Map<String, String> map;
    map = new HashMap<String, String>();

    try {
        map.put("ID", user.getId().toString());
        map.put("email", user.getProperty("email").toString());
        map.put("rel status", user.getProperty("relationship_status")
                .toString());
        map.put("name", user.getName().toString());
        // - requires user_birthday permission
        map.put("birthday", user.getBirthday().toString());
        map.put("gender", user.getProperty("gender").toString());
    } catch (Exception e) {
        // TODO: handle exception
        e.printStackTrace();
        Log.d(TAG,"Info is: "+map);
    }
}}

我在设备上安装了facebook app,但它也没有从该应用程序启动登录。 我是否必须添加不同的内容才能获得用于登录的webview或登录屏幕。

请注意,此代码适用于模拟器: 谢谢你的帮助

1 个答案:

答案 0 :(得分:0)

Facebook SDK最常见的陷阱之一是获取debug.keystore的Android keyhash而不是分发密钥库。

我认为如果您的应用程序在您的模拟器上工作,那是因为您已在Facebook开发人员网站上的Facebook应用程序配置上设置了debug.keystore哈希。

您只需在终端上运行此行:

keytool -exportcert -alias androireleasekey -keystore PATH_TO_THE_RELEASE_KEYSTORE | openssl sha1 -binary | openssl base64

并将其放在调试版旁边的应用配置(Facebook Developers网站)上。

希望它有所帮助! :)