我正在尝试将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);
}
}
有人请告诉我,我在这里犯的是什么错误。谢谢, 哈拉
答案 0 :(得分:1)
我有点面临同样的问题。并更新以下代码来解决它。
public void loginAndPostToWall() {
facebook.authorize(this, PERMISSIONS, Facebook.FORCE_DIALOG_AUTH,
new LoginDialogListener());}