Android facebook sdk 3.0 logout not working? - android

I'm using facebook sdk 3.0 to login my application. login working fine. once i logout my application it's not happening.
my logout code:
Facebook mFb=new Facebook("xxxxxxxx");
mFb.logout(this);
give me some idea to do this.

I have been facing same problem finally i found this fix
public void logout()
{
Session session = Session.getActiveSession();
if (session != null) {
session.closeAndClearTokenInformation();
}
else
{
Session session2 = Session.openActiveSession((Activity)context, false, null);
if(session2 != null)
session2.closeAndClearTokenInformation();
}
Session.setActiveSession(null);
}
I do not know whats going inside but i guess we will have to create new session if it is not found and clear token information for it.
It worked for me. hope it will help.

Try this:
if( mFb.isSessionValid() ) {
mFb.logout(getApplicationContext());
SessionStore.clear(getApplicationContext());
}

Ans: Working fine
Session.openActiveSession(this, true, new Session.StatusCallback() {
#Override
public void call(final Session session, final SessionState state, Exception exception){
// TODO Auto-generated method stub
if(session.isOpened()){
Request.executeMeRequestAsync(session, new Request.GraphUserCallback() {
#Override
public void onCompleted(GraphUser user, Response response) {
// TODO Auto-generated method stub
if(user!=null){
try{
session.close();
session.closeAndClearTokenInformation();
//state.isClosed();
}catch (Exception e) {
Log.e(tag, "getUserIdMethod--->"+e);
}
}
}
});
}
}});

For Facebook use:
LoginManager.getInstance().logOut();
to logout.

did you tried to call it on separate thread:
public void logout()
{
new Thread(){
public void run() {
try {
mFacebook.logout(your_current_class_name.this);
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
};
}.start();
}

Related

Using access token on graph api

I am trying to get JSON data from this url.
As you can see I need access token to get this.
So far I am able to login to facebook and get user access token
`fb.authorize(MainActivity.this, new DialogListener() {
#Override
public void onFacebookError(FacebookError e) {
// TODO Auto-generated method stub
Toast.makeText(MainActivity.this, "OnFbError", Toast.LENGTH_SHORT);
}
#Override
public void onError(DialogError e) {
// TODO Auto-generated method stub
Toast.makeText(MainActivity.this, "OnError", Toast.LENGTH_SHORT);
}
#Override
public void onComplete(Bundle values) {
// TODO Auto-generated method stub
Toast.makeText(MainActivity.this, "OnComplete", Toast.LENGTH_SHORT);
Editor editor=sp.edit();
editor.putString("access_token", fb.getAccessToken());
editor.putLong("access_expires", fb.getAccessExpires());
editor.commit();
try {
updateButtonImage();
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Intent i = new Intent(MainActivity.this, NewsFeed.class);
startActivity(i);
}`
How to use this access token to access the json data?
By using Facebook SDK you can manage authentication and token by the Session class. getAccessToken() method in session class is responsible to provide token.
Session session = Session.getActiveSession();
if (session != null && session.getState().isOpened()){
Log.i("sessionToken", session.getAccessToken());
Log.i("sessionTokenDueDate", session.getExpirationDate().toLocaleString());
}
By using this access token u can get user image as:
https://graph.facebook.com/me/picture?access_token=<your_access_token_here>

How to get Facebook user name in android app

I added Facebook login function to my application. it was successfully connect and logout from the application.
I want to print that login user name in my application.
Can anyone help meto find this?
I used This as example
This is my code.
fbLogin.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
if(fb.isSessionValid()){
try {
fb.logout(getApplicationContext());
updateButtonImage();
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}else{
fb.authorize(MainActivity.this, new String[]{"email"}, new DialogListener() {
#Override
public void onFacebookError(FacebookError e) {
// TODO Auto-generated method stub
Toast.makeText(MainActivity.this, "fbError", Toast.LENGTH_LONG).show();
}
#Override
public void onError(DialogError e) {
// TODO Auto-generated method stub
}
#Override
public void onComplete(Bundle values) {
// TODO Auto-generated method stub
updateButtonImage();
}
#Override
public void onCancel() {
// TODO Auto-generated method stub
Toast.makeText(getApplicationContext(), "onCancel", Toast.LENGTH_LONG).show();
}
});
}
}
});
you need to refer this link facebook sdk
and get information from user object.
Bundle params = new Bundle();
params.putString("fields", "name,first_name,last_name");
friendsRequest.setParameters(params);
// Get current logged in user information
Request meRequest = Request.newMeRequest(session, new Request.GraphUserCallback() {
#Override
public void onCompleted(GraphUser user, Response response) {
FacebookRequestError error = response.getError();
if (error != null) {
Log.e(FriendSmashApplication.TAG, error.toString());
handleError(error, true);
} else if (session == Session.getActiveSession()) {
// Set the currentFBUser attribute
((FriendSmashApplication)getApplication()).setCurrentFBUser(user);
}
}
});
or you can try this example.
http://www.androiddevelopersolution.com/2012/09/android-facebook-integration.html
I would follow this Facebook doc on how to fetch the user's data after he logged into your application.

Add Data to post on facebook wall in android not displaying any message to post

I am using the following code and trying to post message on facebook wall along with the picture url. Both message and picture url is empty. Can any one find out and direct me in the right direction?
btnPostToWall.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
postToWall();
}
});
public void postToWall() {
Bundle params = new Bundle();
params.putString("message","test to post");
params.putString("url", URLEncoder.encode("http://a.espncdn.com/photo/2010/0523/pg2_a_cricket_576.jpg"));
try {
String response = facebook.request("me/feed", params, "POST");
} catch (FileNotFoundException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
} catch (MalformedURLException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
// 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() {
}
});
}
set this permission as public
private String[] PERMISSIONS = new String[]
{ "publish_stream", "read_stream", "offline_access" };
This will check is session exist if yes then it will direct update if not then ask user permission to update on wall.
Button click event
facebookConnector = new FacebookConnector(APP_ID, activity.getParent(),
activity.getApplicationContext(),PERMISSIONS);
if (facebookConnector.getFacebook().isSessionValid())
{
postMessageInThread(position); // see method below
}
else
{
SessionEvents.AuthListener listener = new SessionEvents.AuthListener()
{
#Override
public void onAuthSucceed()
{
postMessageInThread(position);
}
#Override
public void onAuthFail(String error)
{
}
};
SessionEvents.addAuthListener(listener);
facebookConnector.login();
}
This will update your data on wall
private void postMessageInThread(final int position)
{
Thread t = new Thread()
{
public void run()
{
try
{
facebookConnector.postMessageOnWall(
"Your Text Data",
"Your Email URL","Description");
mFacebookHandler.post(mUpdateFacebookNotification);
} catch (Exception ex)
{
Log.e("", "Error sending msg", ex);
}
}
};
t.start();
}
Note: : Here Position is my list record so manage your data according to that.

Saving Facebook profile picture and username Android

I've been working on a simple app for android - and i'm trying to login using Single Sign-On using the Facebook Android SDK.
I'm trying to save the profile picture and username. I'm not quite sure what's wrong - but nothing seems to happen after I Sign In on Facebook.
Here's my code:
public class fbLogin extends Activity {
Facebook facebook = new Facebook("MY-APP-ID");
private SharedPreferences mPrefs;
public boolean flag=false;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.fblogin_screen);
mPrefs=getPreferences(MODE_PRIVATE);
String acces_token=mPrefs.getString("acces_token",null);
long expires=mPrefs.getLong("acces_expires", 0);
if(acces_token!=null)
{
facebook.setAccessToken(acces_token);
}
if(expires!=0)
{
facebook.setAccessExpires(expires);
}
if(!facebook.isSessionValid())
{
facebook.authorize(this, new DialogListener() {
public void onComplete(Bundle values) {
flag=true;
SharedPreferences.Editor editor = mPrefs.edit();
editor.putString("access_token", facebook.getAccessToken());
editor.putLong("access_expires", facebook.getAccessExpires());
editor.commit();
}
public void onFacebookError(FacebookError error) {}
public void onError(DialogError e) {}
public void onCancel() {}
});
} else{
flag=true;
}
if(flag==true)
{
try {
JSONObject me = new JSONObject(facebook.request("me"));
String id=me.getString("id");
String userName=me.getString("username");
ImageView picture;
TextView usr = (TextView)findViewById(R.id.userName);
picture = (ImageView) findViewById(R.id.profilePicture);
URL image_value= new URL("http://graph.facebook.com/" + userName + "/pictures" );
Bitmap profPict=BitmapFactory.decodeStream(image_value.openConnection().getInputStream());
picture.setImageBitmap(profPict);
usr.setText(userName);
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
#Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
facebook.authorizeCallback(requestCode, resultCode, data);
}
What am I doing wrong?
Change this
URL image_value= new URL("http://graph.facebook.com/" + userName + "/pictures" );
to this
URL image_value= new URL("http://graph.facebook.com/" + userName + "/picture" );
Put this:
if(flag==true)
{
try {
JSONObject me = new JSONObject(facebook.request("me"));
String id=me.getString("id");
String userName=me.getString("username");
ImageView picture;
TextView usr = (TextView)findViewById(R.id.userName);
picture = (ImageView) findViewById(R.id.profilePicture);
URL image_value= new URL("http://graph.facebook.com/" + userName + "/pictures" );
Bitmap profPict=BitmapFactory.decodeStream(image_value.openConnection().getInputStream());
picture.setImageBitmap(profPict);
usr.setText(userName);
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
In it's own method called getInfo(), then in your dialog listener call getInfo() like so
facebook.authorize(this, new DialogListener() {
public void onComplete(Bundle values) {
flag=true;
SharedPreferences.Editor editor = mPrefs.edit();
editor.putString("access_token", facebook.getAccessToken());
editor.putLong("access_expires", facebook.getAccessExpires());
editor.commit();
//We're authorized so getInfo();
getInfo();
}
public void onFacebookError(FacebookError error) {}
public void onError(DialogError e) {}
public void onCancel() {}
});
Now, in the Facebook SDK, there's a class called Utility. Set LOGGING = true.
Give it another go and watch the logs. The logs from facebook may say there's a mismatch between your app hash and the facebook app. Copy the hash in the log, go to your facebook app, check the box for android application and paste the hash in the corresponding field.

Android JSch session closes after two commands

I'm attempting to run some commands over ssh from my app. I have a door unlocker and a script that takes lock or unlock.
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
new Thread(new Runnable()
{
public void run()
{
connectSSH();
}
}).start();
}
I setup the session in a separate thread as you cannot do networking in the main UI thread on android.
private void connectSSH()
{
JSch jsch = new JSch();
try {
session = jsch.getSession("door", "my server",22);
} catch (JSchException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
session.setPassword("pass");
session.setConfig("StrictHostKeyChecking", "no");
try {
session.connect(40000);
channel = session.openChannel("exec");
} catch (JSchException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
From here I just have button listeners:
public void unlock(View view) {
((ChannelExec)channel).setCommand("python /home/door/doorUnlocker.py unlock");
try {
channel.connect();
} catch (JSchException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
channel.disconnect();
}
public void lock(View view) {
((ChannelExec)channel).setCommand("python /home/door/doorUnlocker.py lock");
try {
channel.connect();
} catch (JSchException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
channel.disconnect();
}
I can unlock, then lock the door but after that the session just closes so when it attempts to connect to the channel it throws an exception.
Thanks for any help you can provide.

Categories

Resources