Facebook SDK 3.5 : fetch user data example works in emulator but not in real device

丶灬走出姿态 提交于 2019-12-11 09:57:38

问题


I am trying to implement fetch user data example give on developers.facebook.com It works fine on Emulator and displayes data in logcat but when i try it in real device it doesn't open screen for login thats why it directly writes message to Logcat "Logged out"( I guess that means state is closed) following is the code

private void onSessionStateChange(Session session, SessionState state, Exception exception) {

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);
    }
}}

I have facebook app installed in device but it doesn't initiate login from that app also. do I have to add something different in order to get webview or login screen for login.

Note that this code works in emulator: Thanks for the help


回答1:


one of the most common pitfalls of the Facebook SDK is to get the Android keyhash for the debug.keystore but not for the distribution keystore.

I asume that if your app it´s working on your emulator it´s because you have set your debug.keystore hash on your Facebook app configuration on the Facebook Developer site.

You just need to run this line on the terminal:

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

And put it on the app configuration (Facebook Developers site) next to the debug one.

Hope it helps! :)



来源:https://stackoverflow.com/questions/20466006/facebook-sdk-3-5-fetch-user-data-example-works-in-emulator-but-not-in-real-dev

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!