Integrate with facebook in android - android

I have to integrate with facebook in android application.
Here i have to click the button means the fb login page is opened.if i have to entered the correct login page means the post to wall is opened.
Here i have to write the some text means it is post to fb wall successfully.But i wish to pass the string value here.that string value is display on that edittext box also post on wall.how can i do ???
I have using below code:
public class AndroidFacebookConnectActivity extends Activity {
// Your Facebook APP ID
private static String APP_ID = "xxxxx"; // Replace with your App ID
// Instance of Facebook Class
private Facebook facebook = new Facebook(APP_ID);
private AsyncFacebookRunner mAsyncRunner;
String FILENAME = "AndroidSSO_data";
private SharedPreferences mPrefs;
Button btnFbLogin;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
btnFbLogin = (Button) findViewById(R.id.btn_fblogin);
mAsyncRunner = new AsyncFacebookRunner(facebook);
btnFbLogin.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Log.d("Image Button", "button Clicked");
loginToFacebook();
}
});
}
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);
postToWall();
Log.d("FB Sessions", "" + facebook.isSessionValid());
}
if (expires != 0) {
facebook.setAccessExpires(expires);
}
if (!facebook.isSessionValid()) {
facebook.authorize(this,
new String[] { "email", "publish_stream" },
new DialogListener() {
#Override
public void onCancel() {
// Function to handle cancel event
}
#Override
public void onComplete(Bundle values) {
SharedPreferences.Editor editor = mPrefs.edit();
editor.putString("access_token",
facebook.getAccessToken());
editor.putLong("access_expires",
facebook.getAccessExpires());
editor.commit();
btnFbLogin.setVisibility(View.INVISIBLE);
postToWall();
}
#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);
}
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) {
String sh="Krish";
Bundle params = new Bundle();
params.putString("message", sh);
params.putString("name", sh);
}
#Override
public void onCancel() {
}
});
}}
Here how can i post the String sh="Krish"; value on FB wall.how can i display these Krish value on fb text.please give me some suggestions...
EDIT:
This is my updated onComplete code:
#Override
public void onComplete(Bundle values) {
String sh="Krish";
Bundle parameters = new Bundle();
parameters.putString("caption",sh);
}
But now also am getting same error ly.how can i resolve these .please help me.
Here i have to wrote the text Hi.These Hi is posted successfully.But i wish to display the Krish on that edittext box dynamically.How can i do ????

Try adding this to your OnComplete
params.putString("caption",sh);

Are you sure that you are using Facebook SDK 3?
According to these lines:
private Facebook facebook = new Facebook(APP_ID);
private AsyncFacebookRunner mAsyncRunner;
I think you are using old SDK which so many of methods are deprecated.

Related

Facebook Login Issue in android

I'm implementing Facebook Login in my android app . The problem is When I run the app , it redirects to the facebook login page on the click of a button in my main activity . When I enter the credentials , a white page is displayed and nothing happens . Also , if I run the app a second time , only the blank page shows up . I tried to toggle on/off the single sign on functionality in the developer settings page of facebook , but it didn't help . Here's my code of the Facebook Login activity .
public class FacebookLogin extends Activity {
// Your Facebook APP ID
private static String APP_ID = "xxxxxxx"; // Replace with your App
// ID
// Instance of Facebook Class
private Facebook facebook = new Facebook(APP_ID);
private AsyncFacebookRunner mAsyncRunner;
String FILENAME = "AndroidSSO_data";
private SharedPreferences mPrefs;
#SuppressWarnings("deprecation")
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mAsyncRunner = new AsyncFacebookRunner(facebook);
ConnectivityManager cm = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo networkInfo = cm.getActiveNetworkInfo();
if (networkInfo != null && networkInfo.isConnected()) {
// Start AsyncTASK here
loginToFacebook();
} else {
DialogClass
.createDAlertDialog(
FacebookLogin.this,
"Please Check Internet Connection \n Please Turn on Internet",
true);
}
}
/**
* 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);
Log.d("FB Sessions", "" + facebook.isSessionValid());
}
if (expires != 0) {
facebook.setAccessExpires(expires);
}
if (!facebook.isSessionValid()) {
facebook.authorize(this,
new String[] { "email", "publish_stream" },
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
SharedPreferences.Editor editor = mPrefs.edit();
editor.putString("access_token",
facebook.getAccessToken());
editor.putLong("access_expires",
facebook.getAccessExpires());
editor.commit();
}
public void onAuthFail(String error) {
Log.e("FB", "AuthFailed: " + error);
Toast.makeText(FacebookLogin.this, "Auth fail",
Toast.LENGTH_LONG).show();
startActivity(new Intent(FacebookLogin.this,
SignIn.class));
}
public void onAuthSucceed() {
Log.e("FB", "AuthSucceed");
Toast.makeText(FacebookLogin.this, "Auth success",
Toast.LENGTH_LONG).show();
startActivity(new Intent(FacebookLogin.this,
Logout.class));
}
#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);
}
/**
* Function to show Access Tokens
* */
public void showAccessTokens() {
String access_token = facebook.getAccessToken();
Toast.makeText(getApplicationContext(),
"Access Token: " + access_token, Toast.LENGTH_LONG).show();
}
/**
* Function to Logout user from Facebook
* */
#SuppressWarnings("deprecation")
public void logoutFromFacebook() {
mAsyncRunner.logout(this, new RequestListener() {
#Override
public void onComplete(String response, Object state) {
Log.d("Logout from Facebook", response);
if (Boolean.parseBoolean(response) == true) {
runOnUiThread(new Runnable() {
#Override
public void run() {
}
});
}
}
#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) {
}
});
}
}
I implemented that same for windows phone application. I used to face the same problem. But got it correct.
https://github.com/sravich/MvvmCross-Facebook-Login
You could use this link to check the code and run it in similar fashion for android platform. Logic is the same.

Unable to use facebook login in android device

I have same issue mentioned in this stack overflow question. But couldn't find the solution. My problem is when I use facebook login in my android device(which is not installed facebook app), my app works as expected. If I install the facebook application my code doesn't work anymore and It throws the error "Login failed. Please contact the maker of this app and ask them to report issue #1118578 to Facebook". My code is posted below
private Facebook facebook = new Facebook(APP_ID);
private AsyncFacebookRunner mAsyncRunner;
String FILENAME = "AndroidSSO_data";
private SharedPreferences mPrefs;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mAsyncRunner = new AsyncFacebookRunner(facebook);
loginToFacebook();
}
#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 (expires != 0) {
facebook.setAccessExpires(expires);
}
if (!facebook.isSessionValid()) {
facebook.authorize(this,
new String[] { "email", "publish_stream" },
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
SharedPreferences.Editor editor = mPrefs.edit();
editor.putString("access_token",
facebook.getAccessToken());
editor.putLong("access_expires",
facebook.getAccessExpires());
editor.commit();
getProfileInformation();
} }); } }
#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
final String name = profile.getString("name");
// getting email of the user
final String email = profile.getString("email");
runOnUiThread(new Runnable() {
#Override
public void run() {
// getProfileInformation();
String arr[] = name.split(" ", 2);
String name1 = arr[0];
String name2 = arr[1];
Intent intent = new Intent(FacebookLogin.this, UserAccount.class);
/*Sending some arguments*/
Bundle bundle = new Bundle();
bundle.putString("UserName",name1);
bundle.putString("Id", email);
intent.putExtras(bundle);
logoutFromFacebook();
startActivity(intent); }
});
} catch (JSONException e) {
e.printStackTrace();
} } }); }
#SuppressWarnings("deprecation")
public void logoutFromFacebook() {
mAsyncRunner.logout(this, new RequestListener() {
#Override
public void onComplete(String response, Object state) {
facebook.setAccessToken(null);
facebook.setAccessExpires(0);
Log.d("Logout from Facebook", response);
if (Boolean.parseBoolean(response) == true) {
// User successfully Logged out
} } }); }
}
Please anyone tell me how can I avoid this error...

Facebook Android Tutorial logout

I am following this tutorial. I got as far as to the point you retrieve the access token from the savedPreferences and then created a logout button. The problem is, it seems I access_token is always correct, no matter if I logout from my app or even from Facebook app.
This is my code:
public class MainActivity extends Activity {
private String TAG = MainActivity.class.getName();
Facebook mFacebook = new Facebook("whatever");
AsyncFacebookRunner mAsyncRunner = new AsyncFacebookRunner(mFacebook);
private SharedPreferences mPrefs;
//UI elements
private ImageButton fbLoginButton;
private Button getInfoButton;
private Button fbLogoutButton;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mPrefs = getPreferences(MODE_PRIVATE);
String access_token = mPrefs.getString("access_token", null);
long expires = mPrefs.getLong("access_expires", 0);
if(access_token != null) {
Log.d(TAG, "Token found. Setting token " + access_token);
mFacebook.setAccessToken(access_token);
}
if(expires != 0) {
Log.d(TAG, "Setting expire " + expires);
mFacebook.setAccessExpires(expires);
}
if(!mFacebook.isSessionValid()) {
mFacebook.authorize(this, new String[] {"email", "read_friendlists" }, new DialogListener() {
#Override
public void onComplete(Bundle values) {
SharedPreferences.Editor editor = mPrefs.edit();
editor.putString("access_token", mFacebook.getAccessToken());
editor.putLong("access_expires", mFacebook.getAccessExpires());
editor.commit();
Log.d(TAG, "Logging you in");
}
#Override
public void onFacebookError(FacebookError error) {
Log.d(TAG, "Facebook error: " + error.getMessage() );
}
#Override
public void onError(DialogError e) {
Log.d(TAG, "Error: " + e.getMessage());
}
#Override
public void onCancel() {
Log.d(TAG, "User Canceled");
}
});
}
else{
Toast.makeText(this, "Already logged in", Toast.LENGTH_LONG).show();
}
fbLoginButton = (ImageButton) findViewById(R.id.buttonFBLogin);
getInfoButton = (Button) findViewById(R.id.buttonGetInfo);
fbLogoutButton = (Button) findViewById(R.id.buttonLogout);
fbLogoutButton.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
doLogout();
}
});
}
protected void doLogout() {
mAsyncRunner.logout(this, new RequestListener() {
#Override
public void onComplete(String response, Object state) {
Log.d(TAG, "Logging you out");
}
#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) {}
});
}
#Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
mFacebook.authorizeCallback(requestCode, resultCode, data);
}
}
I am always getting the Already logged in Toast.Am I doing something wrong?
PS. before retrieving the access_token from sharedpreferences, the app seemed to work fine, so it's not a problem with my API key or my debug key.
Try mFacebook.logout and check reponse value. If logout is successful it will be "true". Works for me.
Don't forget about Single Sign-On feature. If you will be still logged in Facebook app it won't really logout you.

Post text message on facebook wall

I got a problem while posting text message on the wall.
I expected the apps will login to fb and post a text message on the wall, but nothing is posted on my wall.
The following is my code
The login part is the same as the Facebook guide and i just added the permission "publish stream"
public class FB_trial extends Activity {
Facebook facebook = new Facebook("app_id");
private AsyncFacebookRunner mAsyncRunner = new AsyncFacebookRunner(facebook);
private SharedPreferences mPrefs;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_fb_trial);
/*
* The following is tutorial sample code
*/
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);
}
if(expires != 0) {
facebook.setAccessExpires(expires);
}
/*
* Only call authorize if the access_token has expired.
*/
if(!facebook.isSessionValid()) {
facebook.authorize(this, new String[] {"publish_stream"}, new DialogListener() {
#Override
public void onComplete(Bundle values) {
SharedPreferences.Editor editor = mPrefs.edit();
editor.putString("access_token", facebook.getAccessToken());
editor.putLong("access_expires", facebook.getAccessExpires());
editor.commit();
}
#Override
public void onFacebookError(FacebookError error) {}
#Override
public void onError(DialogError e) {}
#Override
public void onCancel() {}
});
}
//end of the login sample code
//post wall fx
Bundle params = new Bundle();
params.putString("message", "Fb testing message");
params.putString("access_token", access_token);
mAsyncRunner = new AsyncFacebookRunner(facebook);
mAsyncRunner.request("me/feed", params, "POST", new RequestListener(){
#Override
public void onComplete(String response, Object state) {
System.out.println("post wall success");
}
#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) {}
}, null);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_fb_trial, menu);
return true;
}
#Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
facebook.authorizeCallback(requestCode, resultCode, data);
}
}
The "wall post success" message is shown in the log (is that means the request is complete?)
but i can't find the post on my wall.
You can use the dialog to post to wall:
Bundle params = new Bundle();
params.putString("link", linkgame);
params.putString("name", subject);
params.putString("description", content);
params.putString("picture", iconUrl);
facebook.dialog(((Activity) ctx).getParent(), "feed", params,
new DialogListener() {
public void onComplete(Bundle values) {
}
public void onFacebookError(FacebookError error) {}
public void onError(DialogError e) {}
public void onCancel() {}
});

Android - Facebook login dialog and progress indicatior keeps popping out upon logging in

I have implemented Facebook capabilities in my Android running application. When the user presses on the Facebook ImageButton, it will start an authentication process (SSO). And then do a post to the user's facebook wall. Everything runs smoothly on the emulator. Able to post on wall and able to view.
However, while testing on a real device, one problem occur after user presses the ImageButton. The ProgressDialog keeps popping out and does not stop. User will need to close program by pressing the Home button on the device.
What could be the problem? I didn't changed any codes in the Facebook.java class
Adapter.java
ImageButton fbBtn = (ImageButton) view.findViewById(R.id.fb);
fbBtn.setOnClickListener(
new Button.OnClickListener() {
public void onClick(View v)
{
taskListener.doAuthentication();
taskListener.postToWall(data[position], text[position], name[position]);
System.out.println(text[position]);
}
}
);
public static interface FBookTaskListener{
public void doAuthentication();
public void postToWall(String data, String text, String name);
}
Activity.java
protected class TheTask extends AsyncTask<Void, Void, MyResultClass >{
protected void onPreExecute() {
dialog = ProgressDialog.show(Activity.this, "Retrieving Information", "Please wait for a few seconds...", true, false);
dialog.setCancelable(true);
}
protected MyResultClass doInBackground(Void... params) {
searchContent();
MyResultClass result = new MyResultClass();
result.mStrings = mStrings;
result.dStrings = dStrings;
result.date = date;
result.name = name;
return result;
}
#Override
protected void onPostExecute(MyResultClass result) {
dStrings = result.dStrings;
mStrings = result.mStrings;
date = result.date;
name = result.name;
LazyAdapter adapter = new Adapter(Activity.this, mStrings, dStrings, name);
list.setAdapter(adapter);
adapter.setTaskListener(new FBookTaskListener(){
public void doAuthentication()
{
// here all your FB authentication related stuff.
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);
}
if(expires != 0) {
facebook.setAccessExpires(expires);
}
/*
* Only call authorize if the access_token has expired.
*/
if(!facebook.isSessionValid()) {
facebook.authorize(Activity.this, new String[] {"publish_stream", "offline_access", "read_stream"}, new DialogListener() {
#Override
public void onComplete(Bundle values)
{
SharedPreferences.Editor editor = mPrefs.edit();
editor.putString("access_token", facebook.getAccessToken());
editor.putLong("access_expires", facebook.getAccessExpires());
editor.commit();
}
#Override
public void onFacebookError(FacebookError e) {
Log.d("FACEBOOK ERROR","FB ERROR. MSG: "+e.getMessage()+", CAUSE: "+e.getCause());
}
#Override
public void onError(DialogError e) {
Log.e("ERROR","AUTH ERROR. MSG: "+e.getMessage()+", CAUSE: "+e.getCause());
}
#Override
public void onCancel() {
Log.d("CANCELLED","AUTH CANCELLED");
}
});
}
}
#Override
public void postToWall(String data, String text, String name) {
postToFacebook(data, text, name);
}
});
dialog.dismiss();
}
}
Add this code in ur activity
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
facebook.authorizeCallback(requestCode, resultCode, data);
}

Categories

Resources