Facebook的SDK 3.5:获取用户数据例如工作在模拟器,但在实际设备(Facebook SD

2019-10-19 02:56发布

我想实现获取用户数据的例子给出developers.facebook.com它工作正常,在模拟器和里显示的数据在logcat的,但是当我在实际设备尝试它,它不确实为登录这就是开屏为什么它直接写入消息的logcat “注销”(我想这意味着状态下关闭)以下是代码

私人无效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应用程序,但它不会从应用程序启动的登录也。 我必须补充一点,以获得的WebView或登录屏幕登录不同。

请注意,这个代码在模拟器:感谢您的帮助

Answer 1:

Facebook的SDK中最常见的错误之一是获得了debug.keystore但不能用于分配密钥存储在Android keyhash。

我asume,如果您的应用程序it's你的模拟器的工作it's,因为你已经确立了自己debug.keystore散列上Facebook的开发者网站Facebook应用程序的配置。

你只需要运行在终端上这一行:

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

并把它放在应用程序的配置(在Facebook开发者网站)旁边的调试之一。

希望能帮助到你! :)



文章来源: Facebook SDK 3.5 : fetch user data example works in emulator but not in real device