Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to stay connected to facebook in an Android application

I am working on an application where I need to integrate the social functionality of Facebook.

In my application there is a button that connects the user to his Facebook profile. When the user press this button I'm only open a webview with the Facebook site, with the user Facebook page. Now lets say that I know his email and password and I want to connect him automatically, that he not have to enter his email and password every time. I tried to solve it throw my next question, but as you can see with no success.

I tried also persisting cookies with CookieSyncManager, CookieManager and manually handling.

I think I can solve it by changing the url that I sends to the webView, but I don't know which url. (tried http://www.facebook.com/connect/connect_to_external_page_widget_loggedin.php and http://www.facebook.com/plugins/login.php and then concatenate the url of the user Facebook page, for example http://www.facebook.com/UserProfile)

I really appreciate any help!

Thanks.

like image 219
Ofir A. Avatar asked Jun 21 '12 08:06

Ofir A.


People also ask

How do I stay Logged in on Facebook app?

If you want to stay signed in to your account, you can use Facebook's auto-login feature. Check the “Remember password” checkbox when you're signing in and you're all set. With this feature, you can click your profile picture from the login screen to directly sign in without having to enter the password.

How did you integrate the Facebook SDK in Android app?

To use the Facebook SDK in an Android Studio project, add the SDK as a build dependency and import the SDK. Go to Android Studio | New Project | Minimum SDK. Select API 15: Android 4.0. 3 (IceCreamSandwich) or higher and create your new project.

What Apps Connect with Facebook?

Go to Facebook's App Settings page. Then select Apps from the menu. On the apps page, you'll see all the apps where you've logged into Facebook. On mobile, tap the menu bar (bottom-right for iOS, top-right for Android), and select Settings > Account Settings > Apps > Logged in with Facebook.


2 Answers

You can not manually log in the user to facebook. That is the whole point of oAuth, the process should be transparent to your app and when the user is done with authentication your app will only get an AccessToken.

From that point on, an AccessToken can stay alive with you almost for ever.

It will be invalid when :

  1. Session expires

    This can be solved by adding a call to your onResume() assuming you are using the Facebook Android SDK for authentication/integration of facebook. When this call is successful, your token will be valid for 60 days.

    public void onResume() {    
        super.onResume();
        facebook.extendAccessTokenIfNeeded(this, null);
    }
    
  2. User changes his password

  3. User de-authorizes your app

  4. User logs out of Facebook

    You can do nothing about these three cases! Your token will be invalidated and you will have to ask the user to re-authenticate using the normal flow.

Follow this tutorial to integrate Facebook inside your Android app

like image 102
Sherif elKhatib Avatar answered Oct 15 '22 16:10

Sherif elKhatib


There's a way to use the SDK authentication instead of the SSO as discussed here: How to disable Facebook single sign on for android - Facebook-android-sdk.
But I think that it just results in a bad user experience since the user will need to enter his email/password which is not a fun task to do with most mobile devices.

If the user has the facebook application (katana) installed (which means the use of SSO), then you should be able to just open it with the user profile by using an intent.
I've never done it before, but from these two threads:
launch facebook app from other app
and
Open a facebook page from android app
it looks like you can do something like:

Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setClassName("com.facebook.katana", "com.facebook.katana.ProfileTabHostActivity");
this.startActivity(intent);
like image 2
Nitzan Tomer Avatar answered Oct 15 '22 16:10

Nitzan Tomer