Facebook connect function Logout with android sdk - android

My code is as below:
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() {
// make Login button visible
btnFbLogin.setVisibility(View.VISIBLE);
// making all remaining buttons invisible
btnFbGetProfile.setVisibility(View.INVISIBLE);
btnPostToWall.setVisibility(View.INVISIBLE);
btnShowAccessTokens.setVisibility(View.INVISIBLE);
btnFbLogout.setVisibility(View.INVISIBLE);
}
});
}
}
#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) {
}
});
}
But it does not work and show's an error validating access Token. Can anyone give me a solution for this?

The reason why the error message says "invalid application id" is because you are either not providing an app id or you are providing an invalid app id.
You have to make sure you instantiate Facebook in the beginning of your code in the following manner
Facebook facebook = new Facebook(YOUR_APP_ID_HERE);
AsyncFacebookRunner mAsyncRunner = new AsyncFacebookRunner(facebook);

Related

Android get facebook username exception only the original thread that created a view hierarchy

i working facebook api in android. i successfully can sing in in facebook and try to check facebook user name
i wrote some code but i have
only the original thread that created a view hierarchy
exception
this is a my code:
public void LoginFacebook() {
mFacebook.authorize(this, mPermissions, new LoginDialogListener());
}
private final class LoginDialogListener implements DialogListener {
public void onComplete(Bundle values) {
SessionStore.save(mFacebook, getApplicationContext());
getProfileInformation();
}
public void onCancel() {
SessionEvents.onLoginError("Action Canceled");
}
#Override
public void onFacebookError(FacebookError error) {
SessionEvents.onLoginError(error.getMessage());
}
#Override
public void onError(DialogError error) {
SessionEvents.onLoginError(error.getMessage());
}
}
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
mFacebook.authorizeCallback(requestCode, resultCode, data);
}
#SuppressWarnings("deprecation")
#SuppressLint("SdCardPath")
public void getProfileInformation() {
mAsyncRunner.request("me", new BaseRequestListener() {
#SuppressLint("CommitPrefEdits")
#Override
public void onComplete(String response, Object state) {
Log.e("Profile", response);
String json = response;
try {
JSONObject profile = new JSONObject(json);
facebook_userid = profile.getString("id");
facebook_username = profile.getString("name");
facebook_username = facebook_username.replace("%20", " ");
fb_name.setText(facebook_username);
} 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) {
}
});
}
what am i doing wrong. i can sing in but i can't to show username in textview.
if anyone knows solution please help me
thanks
In your onComplete method you're trying to set the text of a UI element. Your onComplete method is getting called from a background(non main/UI thread). Any time you make a UI change, it must be on the UI thread. Try inserting a runOnUIThread call in your onComplete like this:
runOnUiThread(new Runnable() {
public void run() {
fb_name.setText(facebook_username);
}
});
Note: you will need to make variables declared outside the runnable final (ex: facebook_username)
Also note: most (if not all) web based calls will return on a background thread so you might need to use runOnUIThread frequently.

Android facebook-connect-logout method is not working

Sir this method i am using but this is not working
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) {
// User successfully Logged out
}
}
All you need is to kill authentication cookies. Try-facebook.logout(getApplicationContext());

facebook sdk in android app, only able to login, but not able to logout?

in facebook sdk 3.0 logout not working
I have already tried
1.
Facebook mFb=new Facebook("xxxxxxxx");
mFb.logout(this);
2.
if (Session.getActiveSession() != null)
{
Session.getActiveSession().closeAndClearTokenInformation();
}
Session.setActiveSession(null);
Hope this may help you...
public void logoutFromFB()
mAsyncRunner.logout(this, new RequestListener() {
#Override
public void onComplete(String response, Object state) {
Log.d("Logout from FB", response);
if (Boolean.parseBoolean(response) == true) {
// User successfully Logged 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) {
}
});
}
You can do this
clearUserPrefs(); // user prefrs
session.closeAndClearTokenInformation();
and invoke your facebook login activity
UPDATE:
You could use the traditional google account manager service to logout
AccountManager manager = (AccountManager) getSystemService(ACCOUNT_SERVICE);
Account[] accountsList = manager.getAccountsByType(ACCOUNT_TYPE); // ACCOUNT_TYPE = com.facebook.auth.login
for(int i=0;i<accountsList.length;i++)
manager.removeAccount(accountsList[i], null, null);

Why Facebook login page does not appear when I tried to re-login after I logged out in Android?

I followed this tutorial http://www.androidhive.info/2012/03/android-facebook-connect-tutorial/ to connect Facebook to my Android application. Instead of having many buttons, I have a button that will be used for both login and logout.
1) The first time I run the class, I could login and then logout successfully. And when I click on the same button to login again, the login page would not appear, however the Toast text "LOGGING IN" appear, which is after loginToFacebook() function in my if-else. Hence, I assume, it should have run the facebook login page like the first time I run the class. But the login page does not appear.
What did I do wrong? And what should I do?
2) And how do I display the username in String fbLoggedIn after logged in instead of the text "CONNECTED!!" ?
public class FacebookActivity extends Activity{
private static String APP_ID = "";
private Facebook facebook;
private AsyncFacebookRunner mAsyncRunner;
String FILENAME = "AndroidSSO_data";
private SharedPreferences mPrefs;
private Button backButton;
private String name = "CONNECTED!!";
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.settings_share);
facebook = new Facebook(APP_ID);
mAsyncRunner = new AsyncFacebookRunner(facebook);
RelativeLayout fbButton = (RelativeLayout) findViewById(R.id.fbLayout);
fbButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
if (!facebook.isSessionValid())
{
System.out.println("Not Connected. Clicked and Login.");
loginToFacebook();
Toast.makeText(getApplicationContext(), "LOGGING IN", Toast.LENGTH_LONG).show();}
else
{
System.out.println("Connected. Logged Out.");
logoutFromFacebook();
Toast.makeText(getApplicationContext(), "LOGGED OUT", Toast.LENGTH_LONG).show();}
}
});
}
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);
}
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();
TextView fbUser = (TextView) findViewById(R.id.fbUser);
fbUser.setVisibility(View.VISIBLE);
String fbLoggedIn = name;
fbUser.setText(fbLoggedIn);
Toast.makeText(getApplicationContext(), "LOGGED IN AS " + name, Toast.LENGTH_LONG).show();
}
#Override
public void onError(DialogError error) {
// Function to handle error
}
#Override
public void onFacebookError(FacebookError fberror) {
// Function to handle Facebook errors
}
});
}
}
public void getProfileInformation() {
mAsyncRunner.request("me", new RequestListener() {
#Override
public void onComplete(String response, Object state) {
Log.d("Profile", response);
String json = response;
try {
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() {
Toast.makeText(getApplicationContext(), "Name: " + name + "\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) { }
});
}
//logout from Facebook
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) {
// User successfully Logged 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) { }
});
}
Just to be clearer, I set my if-else like this (extracted from the complete codes above).
RelativeLayout fbButton = (RelativeLayout) findViewById(R.id.fbLayout);
fbButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
if (!facebook.isSessionValid())
{
System.out.println("Not Connected. Clicked and Login.");
loginToFacebook();
Toast.makeText(getApplicationContext(), "LOGGING IN", Toast.LENGTH_LONG).show();}
else
{
System.out.println("Connected. Logged Out.");
logoutFromFacebook();
Toast.makeText(getApplicationContext(), "LOGGED OUT", Toast.LENGTH_LONG).show();}
}
});
This is the behavior that Facebook wants you to take with their API.Your login method checks to see if you have already signed in once and that the facebook session is active. If the session is active then it sets the properties of the facebook object and connects it behind the sences so that the login page does not show again.
So you're not doing anything wrong, it's suppose to happen this way.

Android Facebook Logout Error

I give the user the option to logout in my android app. Sometimes the logout process is stopped by the following errors.
08-18 00:44:25.253: W/System.err(510): com.facebook.android.FacebookError:auth.expireSession failed
08-18 00:44:25.302: W/System.err(510): at com.facebook.android.AsyncFacebookRunner$1.run(AsyncFacebookRunner.java:86)
08-18 00:44:25.302: W/FacebookError(510): com.facebook.android.FacebookError: auth.expireSession failed
Here is my Logout code.
private void logout() {
mAsyncRunner = new AsyncFacebookRunner(facebook);
mAsyncRunner.logout(this, new RequestListener() {
#Override
public void onComplete(String response, Object state)
{
SessionStore.clear(home.this); // clear token and expire value
Editor editor = Prefs.edit();
editor.clear();
editor.commit();
cancelNotification();
}
#Override
public void onIOException(IOException e, Object state) {
Log.d("IOExcenption", e.toString());
}
#Override
public void onFileNotFoundException(FileNotFoundException e,
Object state) {
Log.d("FileNotFoundException", e.toString());
}
#Override
public void onMalformedURLException(MalformedURLException e,
Object state) {
Log.d("MalformedUrlExpection", e.toString());
}
#Override
public void onFacebookError(FacebookError e, Object state) {
e.printStackTrace();
Log.w("FacebookError", e.toString());
}
});
Anyone have this problem? Or see something wrong?

Categories

Resources