0

我正在尝试将 facebook 集成到一个 android 应用程序中。我已将代码添加到 facebook 授权中(按照此处http://developers.facebook.com/docs/mobile/android/build/的指南执行所有步骤)。每当 facebook 授权发生时,onActivityResult 将被回调,但数据为 null,resultCode 为 0(零/RESULT_CANCELED)。

这是我的代码:

主要活动类

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

MCAndroidFBIntegration.java

公共类 MCAndroidFBIntegration 实现 IFacebookIntegration {

private static final String APP_ID = APP_ID_NUMBER;
private static Facebook facebook;
private AsyncFacebookRunner mAsyncFBRunner;
public Preferences mPrefs;
private Context ctx;
private static MCAndroidFBIntegration mcAndroidFBIntegration;

public MCAndroidFBIntegration(Context ctx) {
    this.ctx = ctx;
}

@Override
public void integrate() {
    // Integrate with Facebook
    facebook = new Facebook(APP_ID);
    mAsyncFBRunner = new AsyncFacebookRunner(facebook);
    loginToFacebook();      
}

private void loginToFacebook() {
    mPrefs = GamePreference.getPreferences();
    String accessToken = mPrefs.getString(Config.ACCESS_TOKEN, null);
    long expires = mPrefs.getLong(Config.ACCESS_EXPIRES, 0);

    if (accessToken != null) {
        facebook.setAccessToken(accessToken);
    }

    if (expires != 0) {
        facebook.setAccessExpires(expires);
    }

    if (!facebook.isSessionValid()) {
        facebook.authorize((Activity) ctx, new String[] { "publish_stream",
                "read_stream", "offline_access"}, new DialogListener() {

            @Override
            public void onCancel() {
                // Function to handle cancel event
                System.out.println("Facebook:: event cancelled");
            }

            @Override
            public void onComplete(Bundle values) {
                // Function to handle complete event
                // Edit Preferences and update facebook acess_token
                System.out
                        .println("Facebook:: setting access token and expire time in preferences");
                User user = User.getUser();
                if (user != null) {
                    user.setAccessToken(facebook.getAccessToken());
                    user.setAccessExpires(facebook.getAccessExpires());
                    user.updatePrefs();
                    mPrefs.flush();
                }
            }

            @Override
            public void onError(DialogError error) {
                // Function to handle error
                System.out.println("Facebook:: Error - "
                        + error.getLocalizedMessage());

            }

            @Override
            public void onFacebookError(FacebookError fberror) {
                // Function to handle Facebook errors
                System.out.println("Facebook:: fbError - code: "
                        + fberror.getErrorCode() + "  message: "
                        + fberror.getLocalizedMessage());
            }

        });
    }
}

public static void authorizeCallback(int requestCode, int resultCode,
        Intent data) {
    if (data != null) {
        String error = data.getStringExtra("error");
        if (error != null) {
            String description = data.getStringExtra("error_description");
            System.out.println("Facebook:: error desc - " + description);
        } else {
            String token = data.getStringExtra("access_token");
            String expires = data.getStringExtra("expires_in");
            System.out.println("Facebook:: call back access token - "
                    + token);
            System.out.println("Facebook:: call back expires - " + expires);
        }
    }
    facebook.authorizeCallback(requestCode, resultCode, data);
}

}

凸轮有人请告诉我我在这里做的错误是什么。

谢谢, 卡马尔

4

1 回答 1

1

我有点面临同样的问题。并更新下面的代码来解决它。

public void loginAndPostToWall() {
  facebook.authorize(this, PERMISSIONS, Facebook.FORCE_DIALOG_AUTH,
        new LoginDialogListener());}
于 2012-08-13T07:33:52.143 回答