2014-03-28 127 views
0

我正在开发一个应用程序在Android中发布消息在用户Facebook墙壁。很多示例和链接都可用,但问题是。发布消息在Facebook墙上不显示登录页面

在我的应用程序中,用户将只在安装时登录到他/她的Facebook帐户。然后,每当他/她按下特定按钮自定义消息时,必须在Facebook墙上发布消息而不要求再次登录。

在可用示例中,登录页面将出现,用户必须重新登录。帮助...

编辑:我再次澄清我的想法。我的应用程序是发布用户位置到Facebook墙,以获得帮助,当用户按下Button.This用作安全应用程序。所以它不公平登录Facebook以在使用时间发布消息。

如何做到这一点请帮助.. 谢谢。

+0

如果用户在Facebook网站上更改密码会怎么样? –

+0

您需要使用[用户访问令牌](https://developers.facebook.com/docs/facebook-login/access-tokens/#usertokens)。短暂标记通常具有大约“小时”或'two',而长寿命的令牌通常具有大约60天的使用期限。使用Facebook移动SDK的移动应用获得长寿命令牌 – Anirudha

+0

Brontok:必须显示密码错误消息。 – user3349680

回答

0

您应该在SharedPreferences中保存登录名和密码。

SharedPreferences.Editor editor = mSharedPreferences.edit(); 
editor.putString("access_token",facebook.getAccessToken()); 
editor.putLong("access_expires",facebook.getAccessExpires()); 
editor.commit(); 

this for SharedPreferences

+0

@ user3349680,https: //dl.dropboxusercontent.com/u/192937658/FacebookExample.zip下载此示例并在两个地方(LoginFacebookActivity.java,MainActivity.java)替换您的Facebook APP_ID,希望这会对您有所帮助。 – Shiva

+0

@ user3349680,嗨接受或回复答案。你的问题清除了吗? – Shiva

0

我张贴下面的代码,它进行登录至Facebook,发布到Facebook墙,呈现出轮廓影像,显示好友列表和注销....希望你会喜欢:)

// Add this code to your MainActivity 
package com.example.abc; 

import java.io.File; 
import java.io.FileNotFoundException; 
import java.io.IOException; 
import java.net.MalformedURLException; 
import java.net.URL; 
import java.util.ArrayList; 

import org.json.JSONArray; 
import org.json.JSONException; 
import org.json.JSONObject; 

import android.widget.Button; 
import android.app.Activity; 
import android.content.Intent; 
import android.content.SharedPreferences; 
import android.graphics.Bitmap; 
import android.graphics.BitmapFactory; 
import android.os.Bundle; 
import android.util.Log; 
import android.view.View; 
import android.widget.ArrayAdapter; 
import android.widget.Button; 
import android.widget.ImageView; 
import android.widget.ListView; 
import android.widget.TextView; 
import android.widget.Toast; 

import com.facebook.android.AsyncFacebookRunner; 
import com.facebook.android.AsyncFacebookRunner.RequestListener; 
import com.facebook.android.DialogError; 
import com.facebook.android.Facebook; 
import com.facebook.android.Facebook.DialogListener; 
import com.facebook.android.FacebookError; 
import com.facebook.android.Util; 

import android.app.Activity; 
import android.view.Menu; 

public class MainActivity extends Activity { 

// Your Facebook APP ID 
private static String APP_ID = "your app id"; // Replace with your App 
                // ID 
ArrayList<String> friends_list; 
ListView lv; 
// Instance of Facebook Class 
public static Facebook facebook = new Facebook(APP_ID); 
public static AsyncFacebookRunner mAsyncRunner; 
String FILENAME = "AndroidSSO_data"; 
public SharedPreferences mPrefs; 
SharedPreferences.Editor editor; 
public static String username; 
// Buttons 
static Button btnFbLogin; 
static Button btnFbGetProfile; 
static Button btnPostToWall; 
static Button btnShowFriends; 
static Button btnLogout; 
ArrayAdapter<String> adapter; 
ImageView user_picture; 
URL img_value = null; 
@SuppressWarnings("deprecation") 
@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 
    user_picture= (ImageView)findViewById(R.id.imageView1); 
    btnFbLogin = (Button) findViewById(R.id.btn_fblogin); 
    btnFbGetProfile = (Button) findViewById(R.id.btn_get_profile); 
    btnPostToWall = (Button) findViewById(R.id.btn_fb_post_to_wall); 
    btnShowFriends = (Button) findViewById(R.id.btn_show_friends); 
    btnLogout = (Button) findViewById(R.id.btn_logout); 
    mAsyncRunner = new AsyncFacebookRunner(facebook); 
    friends_list = new ArrayList<String>(); 
    lv = (ListView) findViewById(R.id.list); 

    /** 
    * Login button Click event 
    * */ 
    btnFbLogin.setOnClickListener(new View.OnClickListener() { 

     @Override 
     public void onClick(View v) { 
      Log.d("Image Button", "button Clicked"); 
      loginToFacebook(); 
     } 
    }); 

    /** 
    * Getting facebook Profile info 
    * */ 
    btnFbGetProfile.setOnClickListener(new View.OnClickListener() { 

     @Override 
     public void onClick(View v) { 
      // getFriends(); 
      getProfileInformation(); 

     } 
    }); 
    /** 
    * Getting Friends info 
    * */ 
    btnShowFriends.setOnClickListener(new View.OnClickListener() { 

     @Override 
     public void onClick(View v) { 
      mAsyncRunner 
        .request("me/friends", new FriendsRequestListener()); 

     } 
    }); 

    /** 
    * Posting to Facebook Wall 
    * */ 
    btnPostToWall.setOnClickListener(new View.OnClickListener() { 

     @Override 
     public void onClick(View v) { 
      postToWall(); 
     } 
    }); 
    btnLogout.setOnClickListener(new View.OnClickListener() { 

     @Override 
     public void onClick(View v) { 
      // getFriends(); 
      logoutFromFacebook(); 
      Intent intent = new Intent(MainActivity.this, MainActivity.class); 
      startActivity(intent); 

     } 
    }); 

} 

/** 
* Function to login into facebook 
* */ 
@SuppressWarnings("deprecation") 
public void loginToFacebook() { 

    mPrefs = getPreferences(MODE_PRIVATE); 
    String access_token = mPrefs.getString("access_token", null); 
    long expires = mPrefs.getLong("access_expires", 0); 

    if (access_token != null) { 
     facebook.setAccessToken(access_token); 

     btnFbLogin.setVisibility(View.INVISIBLE); 

     // Making get profile button visible 
     btnFbGetProfile.setVisibility(View.VISIBLE); 

     // Making post to wall visible 
     btnPostToWall.setVisibility(View.VISIBLE); 

     btnShowFriends.setVisibility(View.VISIBLE); 

     // Making photos gallery button visible 


      btnLogout.setVisibility(View.VISIBLE); 


      Log.d("FB Sessions", "" + facebook.isSessionValid()); 
     } 

    if (expires != 0) { 
      facebook.setAccessExpires(expires); 
    } 

    if (!facebook.isSessionValid()) { 
     facebook.authorize(this, 
       new String[] { "email", "publish_stream", "user_photos", "friends_photos" }, 
       new DialogListener() { 

        @Override 
        public void onCancel() { 
         // Function to handle cancel event 
        } 

        @Override 
        public void onComplete(Bundle values) { 
         // Function to handle complete event 
         // Edit Preferences and update facebook acess_token 
         editor = mPrefs.edit(); 
         editor.putString("access_token", 
           facebook.getAccessToken()); 
         editor.putLong("access_expires", 
           facebook.getAccessExpires()); 
         editor.commit(); 

         // Making Login button invisible 
          btnFbLogin.setVisibility(View.INVISIBLE); 

         // Making logout Button visible 
        btnFbGetProfile.setVisibility(View.VISIBLE); 

         btnShowFriends.setVisibility(View.VISIBLE); 

         // Making post to wall visible 
         btnPostToWall.setVisibility(View.VISIBLE); 

         // Making show access tokens button visible 


         btnLogout.setVisibility(View.VISIBLE); 
         getprofilepic(); 

        } 

        @Override 
        public void onError(DialogError error) { 
         // Function to handle error 

        } 

        @Override 
        public void onFacebookError(FacebookError fberror) { 
         // Function to handle Facebook errors 

        } 

       }); 
    } 
} 

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

/** 
* Get Profile information by making request to Facebook Graph API 
* */ 
@SuppressWarnings("deprecation") 
public void getProfileInformation() { 
    mAsyncRunner.request("me", new RequestListener() { 
     @Override 
     public void onComplete(String response, Object state) { 
      Log.d("Profile", response); 
      String json = response; 
      try { 
       // Facebook Profile JSON data 
       JSONObject profile = new JSONObject(json); 

       // getting name of the user 
       username = profile.getString("username"); 

       // getting email of the user 
       final String email = profile.getString("email"); 

       runOnUiThread(new Runnable() { 

        @Override 
        public void run() { 
         Toast.makeText(getApplicationContext(), 
           "Name: " + username + "\nEmail: " + email, 
           Toast.LENGTH_LONG).show(); 
        } 

       }); 

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

     @Override 
     public void onIOException(IOException e, Object state) { 
     } 

     @Override 
     public void onFileNotFoundException(FileNotFoundException e, 
       Object state) { 
     } 

     @Override 
     public void onMalformedURLException(MalformedURLException e, 
       Object state) { 
     } 

     @Override 
     public void onFacebookError(FacebookError e, Object state) { 
     } 
    }); 
} 

/** 
* Function to post to facebook wall 
* */ 
@SuppressWarnings("deprecation") 
public void postToWall() { 
    // post on user's wall. 
    facebook.dialog(this, "feed", new DialogListener() { 
     @Override 
     public void onFacebookError(FacebookError e) { 
     } 

     @Override 
     public void onError(DialogError e) { 
     } 

     @Override 
     public void onComplete(Bundle values) { 
     } 

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

} 

/** 
* Function to show Access Tokens 
* */ 
public void showAccessTokens() { 
    String access_token = facebook.getAccessToken(); 

    Toast.makeText(getApplicationContext(), 
      "Access Token: " + access_token, Toast.LENGTH_LONG).show(); 
} 

public void logout() { 
    if (mPrefs != null) { 
     editor.remove("access_token"); 
     editor.remove("access_expires"); 
     editor.commit(); 
     Intent intent = new Intent(this, MainActivity.class); 
     startActivity(intent); 

    } 
} 

private class FriendsRequestListener implements RequestListener { 

    String friendData; 

    // Method runs when request is complete 
    public void onComplete(String response, Object state) { 
     Log.v("", "FriendListRequestONComplete"); 
     // Create a copy of the response so i can be read in the run() 
     // method. 
     friendData = response; 
     Log.v("friendData--", "" + friendData); 
     // Create method to run on UI thread 
     MainActivity.this.runOnUiThread(new Runnable() { 
      public void run() { 
       try { 
        // Parse JSON Data 
        JSONObject json; 
        json = Util.parseJson(friendData); 

        // Get the JSONArry from our response JSONObject 
        JSONArray friendArray = json.getJSONArray("data"); 

        Log.v("friendArray--", "" + friendArray); 

        for (int i = 0; i < friendArray.length(); i++) { 
         JSONObject frnd_obj = friendArray.getJSONObject(i); 
         friends_list.add("Name:"+frnd_obj.getString("name") + " ID:" 
           + frnd_obj.getString("id")); 
        } 

        adapter = new ArrayAdapter<String>(
          getBaseContext(), 
          android.R.layout.simple_list_item_1, 
          android.R.id.text1, friends_list); 

        lv.setAdapter(adapter); 

       } catch (JSONException e) { 
        // TODO Auto-generated catch block 
        e.printStackTrace(); 
       } catch (FacebookError e) { 
        // TODO Auto-generated catch block 
        e.printStackTrace(); 
       } 
      } 
     }); 
    } 

    @Override 
    public void onIOException(IOException e, Object state) { 
     // TODO Auto-generated method stub 

    } 

    @Override 
    public void onFileNotFoundException(FileNotFoundException e, 
      Object state) { 
     // TODO Auto-generated method stub 

    } 

    @Override 
    public void onMalformedURLException(MalformedURLException e, 
      Object state) { 
     // TODO Auto-generated method stub 

    } 

    @Override 
    public void onFacebookError(FacebookError e, Object state) { 
     // TODO Auto-generated method stub 

    } 
} 
@SuppressWarnings("deprecation") 
public void logoutFromFacebook() { 
    mAsyncRunner.logout(this, new RequestListener() { 

    @Override 
    public void onComplete(final String response, Object state) { 
     Thread timer = new Thread() { //new thread   
      public void run() { 
       Boolean b = true; 
       try { 

         editor.remove("access_token"); 
         editor.commit(); 
        sleep(500); 
       Log.d("Logout from Facebook", response); 

         runOnUiThread(new Runnable() { 
         @Override 
         public void run() { 
          // TODO Auto-generated method stub 
          btnFbLogin.setVisibility(View.VISIBLE); 

          // making all remaining buttons invisible 
          btnFbGetProfile.setVisibility(View.INVISIBLE); 
          btnPostToWall.setVisibility(View.INVISIBLE); 
          btnShowFriends.setVisibility(View.INVISIBLE); 
          btnLogout.setVisibility(View.INVISIBLE); 
          adapter.clear(); 
          adapter.notifyDataSetChanged(); 
          Toast.makeText(getApplicationContext(), "You are logged out from Facebook", Toast.LENGTH_LONG).show(); 
         } 
        }); 



        while (b == true); 
       } catch (InterruptedException e) { 
        // TODO Auto-generated catch block 
        e.printStackTrace(); 
       } 
       finally { 
       } 
      }; 
     }; 
     timer.start(); 

    } 

    @Override 
    public void onIOException(IOException e, Object state) { 
    } 

    @Override 
    public void onFileNotFoundException(FileNotFoundException e, 
    Object state) { 
    } 

    @Override 
    public void onMalformedURLException(MalformedURLException e, 
    Object state) { 
    } 

    @Override 
    public void onFacebookError(FacebookError e, Object state) { 
    } 
    }); 
} 


@SuppressWarnings("deprecation") 
public void getprofilepic(){ 
    mAsyncRunner.request("me", new RequestListener() { 
     @Override 
     public void onComplete(String response, Object state) { 
      Log.d("Profile", response); 
      String json = response; 

      try { 

       // Facebook Profile JSON data 
       final JSONObject profile = new JSONObject(json); 
       runOnUiThread(new Runnable() { 
        @Override 
        public void run() { 

       // getting name of the user 
       try { 
        username = profile.getString("username"); 
       } catch (JSONException e1) { 
        // TODO Auto-generated catch block 
        e1.printStackTrace(); 
       } 


try { 
    img_value = new 
    URL("https://graph.facebook.com/"+username+"/picture?type=normal"); 
} catch (MalformedURLException e) { 
    // TODO Auto-generated catch block 
    e.printStackTrace(); 
} 
Bitmap mIcon1 = null; 

try { 
    mIcon1 = 
    BitmapFactory.decodeStream(img_value.openConnection().getInputStream()); 
} catch (IOException e) { 
    // TODO Auto-generated catch block 
    e.printStackTrace(); 
} 
user_picture.setImageBitmap(mIcon1); 
        } 

       }); 

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

      @Override 
      public void onIOException(IOException e, Object state) { 
      } 

      @Override 
      public void onFileNotFoundException(FileNotFoundException e, 
        Object state) { 
      } 

      @Override 
      public void onMalformedURLException(MalformedURLException e, 
        Object state) { 
      } 

      @Override 
      public void onFacebookError(FacebookError e, Object state) { 
      } 
     }); 

} 


} 

//// 此代码到activity_main.xml中

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
xmlns:tools="http://schemas.android.com/tools" 
android:layout_width="fill_parent" 
android:layout_height="fill_parent" 
android:orientation="vertical" > 


<Button 
    android:id="@+id/btn_fblogin" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_gravity="center_horizontal" 
    android:text="Login with Facebook" /> 

<ImageView 
    android:id="@+id/imageView1" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" /> 

<Button 
    android:id="@+id/btn_fb_post_to_wall" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_gravity="center_horizontal" 
    android:text="Post to Wall" 
    android:visibility="gone" /> 

<Button 
    android:id="@+id/btn_get_profile" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_gravity="center_horizontal" 
    android:text="Get Profile Data" 
    android:visibility="gone" /> 

<Button 
    android:id="@+id/btn_show_friends" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_gravity="center_horizontal" 
    android:text="Show Friends" 
    android:visibility="gone" /> 

<Button 
    android:id="@+id/btn_logout" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_gravity="center_horizontal" 
    android:text="Logout" 
    android:visibility="gone" /> 

<ListView 
    android:id="@+id/list" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" > 
</ListView> 

</LinearLayout> 

///并添加Mainfest

<?xml version="1.0" encoding="utf-8"?> 
<manifest xmlns:android="http://schemas.android.com/apk/res/android" 
package="com.example.abc" 
android:versionCode="1" 
android:versionName="1.0" > 

<uses-sdk 
    android:minSdkVersion="8" /> 
<uses-permission android:name="android.permission.INTERNET"/> 
<application 
    android:allowBackup="true" 
    android:icon="@drawable/icon" 
    android:label="@string/app_name" > 
    <activity 
     android:name="com.example.abc.MainActivity" 
     android:label="@string/app_name" > 
     <intent-filter> 
      <action android:name="android.intent.action.MAIN" /> 

      <category android:name="android.intent.category.LAUNCHER" /> 
     </intent-filter> 
</application> 

</manifest> 
+0

感谢您的帮助KamranAfsar ..但先生...我必须发布消息单点登录方法.. – user3349680

相关问题