2012-04-12 58 views
0

我試圖將Facebook集成到Android應用程序。我已將代碼添加到Facebook授權中(根據此處的指導原則,執行了所有步驟)http://developers.facebook.com/docs/mobile/android/build/)。 每當Facebook授權發生時,onActivityResult將被回調,但數據爲null,resultCode爲0(零/ RESULT_CANCELED)。Android的Facebook的授權onActivityResult()總是調用數據爲空和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

我在日誌中總是得到這個** **活動作爲新任務啓動,因此取消活動結果**。這是什麼意思?我搜索了這個網絡,發現這與android:launchMode =「singleInstance」有關。但是,當我改變了這一點,我得到了一些其他錯誤,如驗證失敗,因爲無效的Android應用程序密鑰。我該怎麼辦?我很困惑和疲憊 – Kamal 2012-04-12 08:49:00

+0

嗨Kamal,你設法解決這個問題嗎?我面臨同樣的問題,但非常困惑 – user669231 2012-07-12 15:37:44

回答

1

我還挺面臨同樣的問題。並更新下面的代碼來解決它。

public void loginAndPostToWall() { 
    facebook.authorize(this, PERMISSIONS, Facebook.FORCE_DIALOG_AUTH, 
     new LoginDialogListener());} 
相關問題