我使用來自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);
}