2011-01-12 79 views
0

我使用來自YouTube的圖形api,我面臨一些麻煩。張貼在Facebook牆上總是顯示登錄頁面..問題android

我可以發送消息到牆上,它工作正常。問題是,每次都會顯示登錄頁面,用戶必須點擊ok按鈕才能發送。我想發送它而不顯示該頁面,它應該只在第一次顯示。我使用 共享首選項存儲令牌,但如果在嘗試發送視頻之前,我沒有調用顯示登錄頁面的方法facebook.autorize,則它不起作用。

我做錯了嗎?謝謝你的幫助 。

我的代碼:

protected void onCreate(Bundle savedInstanceState) { 

    super.onCreate(savedInstanceState); 
    setContentView(R.layout.facebook_lay); 

    init(); 

    acessToken = loadAccessToken(); 
    Log.i("teste","Acesstoken depois do load : " + acessToken); 

    if(acessToken == null || acessToken.equals("")){ 
     login(); 
    }else{ 
     Log.i("teste","Session valid com token salva ? " + mFacebook.isSessionValid()); 
     sendVideo(); 
    } 


} 

private void init(){ 
    mFacebook = new Facebook(APP_ID); 
    videoUrl = getIntent().getExtras().getString("videoUrl"); 
    preferences = this.getSharedPreferences(APPLICATION_PREFERENCES, 
      Context.MODE_PRIVATE); 
} 

private void login(){ 
    mFacebook.authorize(this, new String[] {"publish_stream","offline_access"},new AuthorizeListener()); 

} 

public void postOnWall(String msg) { 
    Log.i("teste", "Testing graph API wall post"); 
    try { 
      Bundle parameters = new Bundle(); 
      parameters.putString("message", AUTO_MSG); 
      parameters.putString("link", msg); 
      mFacebook.request("me/feed", parameters, 
      "POST"); 

    } catch(Exception e) { 
     e.printStackTrace(); 
    } 
} 

private void storeToken(String token){ 
    Log.i("teste","storeToken: " + token); 
    Editor editor = preferences.edit(); 
    editor.putString(ACESS_TOKEN, token); 
    editor.commit(); 
} 

private String loadAccessToken() { 
    String token = preferences.getString(ACESS_TOKEN, null); 
    if (token != null && !token.equals("") ) { 
     return token; 
    } else { 
     return null; 
    } 
} 

private void sendVideo(){ 
    dialog = ProgressDialog.show(FacebookView.this, "Sending","Enviando video ..."); 
    new Thread(){ 
     public void run() { 
      postOnWall(videoUrl); 
      dialog.dismiss(); 
      finish(); 
     }; 
    }.start(); 
} 

class AuthorizeListener implements DialogListener{ 

    @Override 
    public void onCancel() { 
     Log.i("teste","Logou candelado!"); 
     finish(); 
    } 

    @Override 
    public void onComplete(Bundle values) { 
     Log.i("teste","Logou com sucesso!"); 
     Log.i("teste", "AcessToken : " + mFacebook.getAccessToken()); 
     Log.i("teste", "AcessToken expires : " + mFacebook.getAccessExpires()); 
     Log.i("teste", "Session valid : " + mFacebook.isSessionValid()); 

     storeToken(mFacebook.getAccessToken()); 
     sendVideo(); 
    } 

    @Override 
    public void onError(DialogError e) { 
     Log.i("teste","Erro ao logar: " + e.getMessage()); 
     finish(); 
    } 

    @Override 
    public void onFacebookError(FacebookError e) { 
     Log.i("teste","FacebookError ao logar: " + e.getMessage()); 
     Log.i("teste","FacebookError ao logar Causa: " + e.getCause()); 
     e.printStackTrace();    
     finish(); 
    } 

} 

@Override 
protected void onActivityResult(int requestCode, int resultCode, Intent data) { 
    // TODO Auto-generated method stub 
    mFacebook.authorizeCallback(requestCode, resultCode, data); 
} 

回答

0

已經找到了答案。只需在facebook對象上設置標記。

acessToken = loadAccessToken(); (「teste」,「Acesstoken depois do load:」+ acessToken); Log.i(「teste」,「Acesstoken depois do load:」+ acessToken);

if(acessToken == null || acessToken.equals("")){ 
    login(); 
}else{ 
    mFacebook.setAccessToken(acessToken); 
    Log.i("teste","Session valid com token salva ? " + mFacebook.isSessionValid()); 
    sendVideo(); 
} 
1
package com.greatup; 

import com.facebook.android.DialogError; 
import com.facebook.android.Facebook; 
import com.facebook.android.Facebook.DialogListener; 
import com.facebook.android.FacebookError; 

import android.app.Activity; 
import android.content.Intent; 
import android.os.Bundle; 

public class MyGreatActivity extends Activity { 
    /** Called when the activity is first created. */ 

    Facebook facebook = new Facebook("208086425868080"); 

    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.main); 

//  facebook.authorize(this, new String[] { "email", "read_stream" },new DialogListener() { 
//    
//   public void onComplete(Bundle values) {} 
// 
//    
//   public void onFacebookError(FacebookError error) {} 
// 
//    
//   public void onError(DialogError e) {} 
// 
//   public void onCancel() {} 
//  }); 

     facebook.dialog(this,"feed", 

        new DialogListener() { 
         @Override 
         public void onComplete(Bundle values) {} 

         @Override 
         public void onFacebookError(FacebookError error) {} 

         @Override 
         public void onError(DialogError e) {} 

         @Override 
         public void onCancel() {} 
        } 
      ); 




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

     facebook.authorizeCallback(requestCode, resultCode, data); 
    } 
}