1

使用 Facebook sdk,如果用户输入凭据,则在 facebook 登录屏幕中,然后应用程序显示 facebook 错误 toast 消息。我如何通过在此处放置 login() 代码成功登录来解决此问题。

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

                    }

                });
    }
4

2 回答 2

0

您正在使用旧的 facebook sdk。所以使用最新的 facebook sdk。对于最新的 facebook sdk,请点击此链接https://developers.facebook.com/docs/getting-started/facebook-sdk-for-android/3.0/

于 2013-08-16T10:50:18.783 回答
0

试试这个工具,它可以帮助你。它并不完美但可以做一些事情,onActivityResult应该小心使用,它必须在活动的 onActivityResult() 和 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);
}
}

如果你喜欢你可以使用这个库:https ://github.com/mgcrea/cordova-facebook-connect

于 2013-08-16T11:04:14.187 回答