1

Working with Facebook sdk ,here in facebook login screen if user enters the credentials then app showing facebook error toast message.how can i fix this isssue by making a succesful login here placing login() code.

protected void loginToFacebook() {
    // TODO Auto-generated method stub
    mPrefs = getPreferences(MODE_PRIVATE);
    String access_token = mPrefs.getString("access_token", null);
    long expires = mPrefs.getLong("access_expires", 0);
    if (access_token != null) {
        facebook.setAccessToken(access_token);
    }

    if (expires != 0) {
        facebook.setAccessExpires(expires);
    }
    if (!facebook.isSessionValid()) {
        facebook.authorize(this,
                new String[] { "email", "publish_stream" },
                new DialogListener() {

                    @Override
                    public void onCancel() {
                        // Function to handle cancel event
                        Toast.makeText(getApplicationContext(), "Login cancel", Toast.LENGTH_LONG).show();
                    }

                    @Override
                    public void onComplete(Bundle values) {
                        // Function to handle complete event
                        // Edit Preferences and update facebook acess_token
                        SharedPreferences.Editor editor = mPrefs.edit();
                        editor.putString("access_token",
                                facebook.getAccessToken());
                        editor.putLong("access_expires",
                                facebook.getAccessExpires());
                        editor.commit();
                    }

                    @Override
                    public void onError(DialogError error) {
                        // Function to handle error
                        Toast.makeText(getApplicationContext(), "error in login", Toast.LENGTH_LONG).show();
                    }

                    @Override
                    public void onFacebookError(FacebookError fberror) {
                        // Function to handle Facebook errors
                        Toast.makeText(getApplicationContext(), "Facebook error", Toast.LENGTH_LONG).show();

                    }

                });
    }

2 Answers2

0

Your are using old facebook sdk. So use latest facebook sdk. For Latest facebook sdk follow this link https://developers.facebook.com/docs/getting-started/facebook-sdk-for-android/3.0/

Venkata Krishna
  • 1,543
  • 1
  • 11
  • 19
0

Try this util it helps you. It is not perfect but can do somehing, onActivityResult should be used carefully, it must be call in Activities' onActivityResult() and below super.onActivityResult():

public final class FacebookUtil {
public static void postToFacebook(final Activity context, final String name, final String caption,
                                  final String description, final String link, final String _img) {
    Session s = Session.getActiveSession();
    if (s != null && s.isOpened()) {
        post(context, name, caption, description, link, _img);
    } else {
        Session.openActiveSession(context, true, new Session.StatusCallback() {

            @Override
            public void call(Session _session, SessionState _state, Exception _exception) {
                if (_session.isOpened()) {
                    post(context, name, caption, description, link, _img);
                }
            }
        });
    }
}

private static void post(Context context, String name, String caption, String description, String link,
                         String imageUrl) {
    Bundle params = new Bundle();
    params.putString("name", name);
    params.putString("caption", caption);
    params.putString("description", description);
    params.putString("link", link);
    if (imageUrl != null) {
        params.putString("picture", imageUrl);
    }
    new WebDialog.FeedDialogBuilder(context, Session.getActiveSession(), params).build().show();
}

public static void login(Activity _context, Session.StatusCallback _callback) {
    Session.openActiveSession(_context, true, _callback);
}

public static void logout() {
    Session session = Session.getActiveSession();
    if (session != null) {
        session.closeAndClearTokenInformation();
    }
}

public static boolean isLoggedIn() {
    Session session = Session.getActiveSession();
    return (session != null && session.getAccessToken() != null && session.getAccessToken().length() > 1);
}

public static void askMe(Request.GraphUserCallback _callback) {
    Session session = Session.getActiveSession();
    if (session != null) {
        Request.executeMeRequestAsync(session, _callback);
    }
}

/**
 * Don't forget to added this function to onActivityResult() of your activities ever, otherwise
 * you can not finish your facebook successfully.
 */
public static void onActivityResult(Activity _activity, int _requestCode, int _resultCode, Intent _data) {
    Session.getActiveSession().onActivityResult(_activity, _requestCode, _resultCode, _data);
}
}

If you like you can use this lib:https://github.com/mgcrea/cordova-facebook-connect

TeeTracker
  • 7,064
  • 8
  • 40
  • 46