2011-10-16 89 views
0

我編碼了一個應用程序,我需要Facebook共享(張貼到牆上)。張貼到牆上的Facebook API

應用程序接縫工作:我收到一個對話框,詢問我是否想在Facebook上分享。它應該認證後,如果沒有,它應該給我一個錯誤信息,它不。
它只是給了我相同的對話框。

有人可以看看我的代碼嗎?

public class ShareOnFacebook extends Activity{ 

    private static final String APP_ID = "138652356232418"; 
    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(APP_ID); 
     restoreCredentials(facebook); 

     requestWindowFeature(Window.FEATURE_NO_TITLE); 

     setContentView(R.layout.facebook_dialog); 

     String facebookMessage = getIntent().getStringExtra("facebookMessage"); 
     if (facebookMessage == null){ 
      facebookMessage = "Wir sind auf dem Hamburger Weinachtsmarkt, kommt 
       doch einen Glühwein mit uns trinken!"; 
     } 

     messageToPost = facebookMessage; 
     } 

     public void doNotShare(View button){ 
     finish(); 
     } 

     public void share(View button){ 
      if (! facebook.isSessionValid()) { 
      loginAndPostToWall(); 
      } 

      else { 
       postToWall(messageToPost); 
      } 
     } 

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

     public void postToWall(String message){ 
      Bundle parameters = new Bundle(); 
       parameters.putString("message", message); 
       facebook.dialog(this, "stream.publish", parameters, new  
        WallPostDialogListener()); 
     } 

     class LoginDialogListener implements DialogListener { 
      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(); 
     } 
    } 

     class WallPostDialogListener implements DialogListener { 
     public void onComplete(Bundle values) { 
        final String postId = values.getString("post_id"); 
        if (postId != null) { 
        showToast("Message posted to your facebook wall!"); 
       } else { 
        showToast("Wall post cancelled!"); 
       } 
       finish(); 
      } 

     public void onFacebookError(FacebookError e) { 
      showToast("Failed to post to wall!"); 
      e.printStackTrace(); 
      finish(); 
     } 

     public void onError(DialogError e) { 
      showToast("Failed to post to wall!"); 
      e.printStackTrace(); 
      finish(); 
     } 

     public void onCancel() { 
      showToast("Wall post cancelled!"); 
      finish(); 
     } 
      } 
     private void showToast(String message){ 
     Toast.makeText(getApplicationContext(), message, Toast.LENGTH_SHORT).show(); 
     } 
     } 

在我的主菜單上有一個按鈕,它有我粘貼的類的意圖。在我的Facebook分享按鈕中,我有一個意圖:

Intent postOnFacebookWallIntent = new Intent(this, ShareOnFacebook.class); 
      postOnFacebookWallIntent.putExtra("facebookMessage", "is integrating stuff again."); 
      startActivity(postOnFacebookWallIntent); 

好的,我會更加清楚。

這個類是照顧我所有的Facebook API的東西。當我將其與我的應用程序或任何應用程序集成時,它會向用戶提供一個對話,詢問您是否要與Facebook共享。如果你按下是,它應該通過Facebook進行身份驗證併發布消息,或者如果失敗,它應該給我一個錯誤信息,而不是。它嘗試驗證,然後給我相同的對話框,它不應該。

現在我的問題是我做錯了什麼,爲什麼它沒有做它應該做的。

+1

現在你的問題沒有代碼;請編輯問題並添加它。 – Adinia

+0

當你說'在我的Facebook分享按鈕中有一個意圖:'時,真的不清楚你的意思。 – Edwin

+0

您是否在Facebook上註冊了應用程序並使用了正確的密鑰? –

回答

0

當您在Facebook上註冊您的應用時,他們需要一個密鑰才能關聯到您的應用。這有點令人困惑,因爲在eclipse中運行應用程序時獲得的密鑰與您用於發佈該應用程序的密鑰不同,密鑰生成將會看起來像即使您輸入了錯誤的密碼也會給出正確的結果,所以你必須非常確定 A)你正在生成正確的密鑰(調試或真實的) 和 B)你正在使用正確的密碼和別名。

我的猜測是你有一個密鑰不匹配,並且只是簡單地通過FB驗證失敗。

這個post覆蓋它非常詳細。

+0

謝謝你的時間 –