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());
Related
I'm trying to integrate Facebook login in my game but since now I couldn't achieve this.
i have followed the instructions in :
https://github.com/libgdx/libgdx/wiki/Interfacing-with-platform-specific-code
And created my interface and implemented it for Android project but still no success. When i call my loginFacebook function , it shows me a circular load screen and my game screen comes back.
public class AndroidFacebookAdapter implements FacebookIntegration {
private String name;
private Activity androidActivity;
public AndroidFacebookAdapter(Activity andAct) {
super();
this.name = "";
this.androidActivity = andAct ;
}
#Override
public void loginFacebook() {
Gdx.app.log("AndroidFacebookAdapter"," Facebook login starts");
//Session.openActiveSession(this, allowLoginUI, callback)
androidActivity.runOnUiThread(new Runnable() {
#Override
public void run() {
Gdx.app.log("AndroidFacebookAdapter"," runOnUiThread runs");
Session.openActiveSession(androidActivity, true, new Session.StatusCallback() {
// callback when session changes state
#Override
public void call(Session session, SessionState state, Exception exception) {
if (session.isOpened()) {
Gdx.app.log("AndroidFacebookAdapter"," session.isOpened returns true ");
// make request to the /me API
Request.newMeRequest(session, new Request.GraphUserCallback() {
// callback after Graph API response with user object
#Override
public void onCompleted(GraphUser user, Response response) {
if (user != null) {
Gdx.app.log("AndroidFacebookAdapter"," Facebook Login Succefull");
name = user.getName();
}
}
}).executeAsync();
}
}
});
}
});
}
#Override
public String getName() {
return name;
}
}
In my Android project i send this interface to the core project .
andAdap = new AndroidFacebookAdapter(this);
initialize(new MyGdxGame(andAdap), config);
And i called it when screen is touched.
game.facebookIntegration.loginFacebook();
In logcat i see :
10-27 13:06:47.988: I/AndroidFacebookAdapter(16774): Facebook login starts
10-27 13:06:47.988: I/AndroidFacebookAdapter(16774): runOnUiThread runs
After i see a circular loading screen (nothing on screen just black) then my game screen comes back again . In which step i made a mistake ?
Thank you for your answers .
EDIT 1 : After realizing i was already signed in my facebook account , i logged out. Then tried again and it was working. It was opening the Facebook app to ask me for credentials but even i enter the right user name / password my "onCompleted" function is not called.
Edit 2 : Now my problem solved , thanks to the tutorial at http://blog.supercookie.co.uk/post/94802569132/facebook-integration-w-libgdx
I just needed to use UiLifecycleHelper class and its implementation. My interface implmenting class now.
public class AndroidFacebookAdapter implements FacebookIntegration {
private String name;
public AndroidLauncher androidActivity;
private final UiLifecycleHelper uiHelper;
public AndroidFacebookAdapter(Activity andAct) {
super();
this.name = "";
this.androidActivity = (AndroidLauncher) andAct ;
this.uiHelper = new UiLifecycleHelper(this.androidActivity, new Session.StatusCallback() {
#Override
public void call(Session session, SessionState sessionState, Exception e) {
}
});
}
private void loginFacebookImp() {
Gdx.app.log("AndroidFacebookAdapter"," Facebook login starts");
androidActivity.runOnUiThread(new Runnable()
{
#Override
public void run()
{
Gdx.app.log("AndroidFacebookAdapter"," runOnUiThread runs");
Session.openActiveSession(androidActivity, true, new Session.StatusCallback()
{
// callback when session changes state
#Override
public void call(Session session, SessionState state, Exception exception)
{
Gdx.app.log("AndroidFacebookAdapter"," openActiveSession call start");
if (session.isOpened())
{
Gdx.app.log("AndroidFacebookAdapter"," session.isOpened returns true ");
// make request to the /me API
Request.newMeRequest(session, new Request.GraphUserCallback()
{
// callback after Graph API response with user object
#Override
public void onCompleted(GraphUser user, Response response) {
Gdx.app.log("AndroidFacebookAdapter"," onCompleted start");
if (user != null) {
Gdx.app.log("AndroidFacebookAdapter"," Facebook Login Succesfull");
Gdx.app.log("AndroidFacebookAdapter"," Facebook name is " + user.getName());
name = user.getName();
}
else
{
Gdx.app.log("AndroidFacebookAdapter"," Facebook Login failed");
}
}
}).executeAsync();
}
else
{
Gdx.app.log("AndroidFacebookAdapter"," session.isOpened() is false");
}
}
});
}
});
}
private boolean isLoggedInImp()
{
return Session.getActiveSession() != null && Session.getActiveSession().isOpened();
}
#Override
public String getName() {
return name;
}
#Override
public void loginFacebook() {
loginFacebookImp();
}
public void onCreate(Bundle savedInstanceState) {
uiHelper.onCreate(savedInstanceState);
}
public void onResume() {
uiHelper.onResume();
}
public void onActivityResult(int requestCode, int resultCode, Intent data) {
uiHelper.onActivityResult(requestCode, resultCode, data);
}
public void onSaveInstanceState(Bundle outState) {
uiHelper.onSaveInstanceState(outState);
}
public void onPause() {
uiHelper.onPause();
}
public void onStop() {
uiHelper.onStop();
}
public void onDestroy() {
uiHelper.onDestroy();
}
#Override
public boolean isLoggedIn() {
return isLoggedInImp();
}
}
As in edit 2 , just add the UiLifecycleHelper android side interface implementing class. Then it is working as expected. For more explanation :
http://blog.supercookie.co.uk/post/94802569132/facebook-integration-w-libgdx
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.
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);
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.
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);