2012-12-14 26 views
11

我想實現Facebook集成到我的Android應用程序,它會關閉並登錄到Facebook罰款,但是當它試圖傳遞訪問令牌迴應用程序它只是返回:Android上的Facebook集成fbconnect斷開的鏈接

該網頁在fbconnect://成功#=的access_token [訪問令牌] 可能暫時無法連接,或者它已永久性地移動到新的 網址。

很明顯,其中[ACCESS TOKEN]是一個很長的字符序列。

我已經得到了正確的App ID,並將密鑰哈希添加到了Facebook。但是我可以錯過什麼過程?

代碼:

public class FacebookActivity extends Activity { 

private static final String APP_ID = "[MY APP ID]"; 
private static final String[] PERMISSIONS = new String[] {"publish_stream"}; 

private static final String TOKEN = "access_token"; 
    private static final String EXPIRES = "expires_in"; 
    private static final String KEY = "facebook-credentials"; 

private Facebook facebook; 
private String messageToPost; 

public boolean saveCredentials(Facebook facebook) { 
     Editor editor = getApplicationContext().getSharedPreferences(KEY, Context.MODE_PRIVATE).edit(); 
     editor.putString(TOKEN, facebook.getAccessToken()); 
     editor.putLong(EXPIRES, facebook.getAccessExpires()); 
     return editor.commit(); 
    } 

    public boolean restoreCredentials(Facebook facebook) { 
     SharedPreferences sharedPreferences = getApplicationContext().getSharedPreferences(KEY, Context.MODE_PRIVATE); 
     facebook.setAccessToken(sharedPreferences.getString(TOKEN, null)); 
     facebook.setAccessExpires(sharedPreferences.getLong(EXPIRES, 0)); 
     return facebook.isSessionValid(); 
    } 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 

    facebook = new Facebook(); 
    restoreCredentials(facebook); 

    requestWindowFeature(Window.FEATURE_NO_TITLE); 

    setContentView(R.layout.activity_facebook); 

    String facebookMessage = getIntent().getStringExtra("facebookMessage"); 
    if (facebookMessage == null){ 
     facebookMessage = "Test wall post"; 
    } 
    messageToPost = facebookMessage; 

      if (! facebook.isSessionValid()) { 
     loginAndPostToWall(); 
    } 
    else { 
     postToWall(messageToPost); 
    } 
} 

public void loginAndPostToWall(){ 
    facebook.authorize(this, APP_ID, PERMISSIONS, new LoginDialogListener()); 
} 

public void postToWall(String message){ 
    Bundle parameters = new Bundle(); 
      parameters.putString("message", message); 
      parameters.putString("description", "topic share"); 
      try { 
       facebook.request("me"); 
     String response = facebook.request("me/feed", parameters, "POST"); 
     Log.d("Tests", "got response: " + response); 
     if (response == null || response.equals("") || 
       response.equals("false")) { 
      showToast("Blank response."); 
     } 
     else { 
      showToast("Message posted to your facebook wall!"); 
     } 
     finish(); 
    } catch (Exception e) { 
     showToast("Failed to post to wall!"); 
     e.printStackTrace(); 
     finish(); 
    } 
} 

class LoginDialogListener implements DialogListener { 
    @Override 
    public void onComplete(Bundle values) { 
     saveCredentials(facebook); 
     if (messageToPost != null){ 
     postToWall(messageToPost); 
    } 
    } 
    public void onFacebookError(FacebookError error) { 
     showToast("Authentication with Facebook failed!"); 
     finish(); 
    } 
    public void onError(DialogError error) { 
     showToast("Authentication with Facebook failed!"); 
     finish(); 
    } 
    public void onCancel() { 
     showToast("Authentication with Facebook cancelled!"); 
     finish(); 
    } 
} 

private void showToast(String message){ 
    Toast.makeText(getApplicationContext(), message, Toast.LENGTH_SHORT).show(); 
} 

} 

新Android開發,所以我敢肯定,這是簡單的東西。

+0

你的問題是什麼?您無法發佈消息 – TNR

+0

那麼這是最終目標,但問題是,當Facebook嘗試使用訪問令牌重定向回我的應用程序時,它會顯示上述消息。 (網頁無法顯示)。因此,我的應用程序無法獲得訪問令牌,因此永遠無法獲得授權。 – anothershrubery

+0

是您的應用程序去facebook應用程序,然後它不提供令牌或nt給你令牌,當你直接從你的應用程序登錄 –

回答

1

我更新了設備上的本地Facebook應用程序,一切正常。

1

似乎本機Facebook應用程序必須是版本2或更高版本。我在1.9.6版本中看到了同樣的問題,升級Facebook應用程序解決了這個問題。