Steps to integrate Login with facebook in your app

Reference This Link



Step 1:
               
Download Facebook SDK and import it to in eclipse.

Step 2:
               
Generate a keyytool for your project or app.
                For Ubuntu : keytool -exportcert -alias androiddebugkey -keystore ~/.android/debug.keystore | openssl sha1 -binary | openssl base64

Step 3:
               
Create app for in facebook.
                Go to developer.facebook into menu -- app -- Create a New app. and click button 'Create app' it will create your app.

Step 4:
               
Than go to 'Settings' and click 'Add Platform' and select android.
                In that provide your project(app) package name and class name and add generated Key Hashes.
                --One more thing dont forgot to add "Contact Email".

Step 5:
               
Than go to 'Status and Review'.
                Enable or On the - Do you want to make this app and all its live features available to the general public?

Step 6:
               
Now you create new project in android or add below things in your porject:
                - Map FacebookSdk in your project add as library project.
                - Add your application id which is generated in developer.facebook in your string.xml as 'app_id'.
                - Add below things in your AndroidManifeat.xml
               
<uses-permission android:name="android.permission.INTERNET"/>
<meta-data android:name="com.facebook.sdk.ApplicationId" android:value="@string/app_id"/>
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<com.facebook.widget.LoginButton
android:id="@+id/authButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:layout_marginTop="30dp"/>
<TextView
android:id="@+id/user_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_margin="10dp"
android:textSize="18sp"/>
</LinearLayout>
view raw main.xml hosted with ❤ by GitHub
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.util.Log;
import android.widget.TextView;
import com.mihir.practice.R;
import java.util.Arrays;
import java.util.List;
public class MainActivity extends FragmentActivity {
private LoginButton loginBtn;
private TextView userName;
private UiLifecycleHelper uiHelper;
private static final List<String> PERMISSIONS = Arrays.asList("publish_actions");
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
uiHelper = new UiLifecycleHelper(this, statusCallback);
uiHelper.onCreate(savedInstanceState);
setContentView(R.layout.main);
userName = (TextView) findViewById(R.id.user_name);
loginBtn = (LoginButton) findViewById(R.id.authButton);
loginBtn.setUserInfoChangedCallback(new UserInfoChangedCallback() {
@Override
public void onUserInfoFetched(GraphUser user) {
if (user != null) {
userName.setText("Hello, " + user.getName());
} else {
userName.setText("You are not logged");
}
}
});
buttonsEnabled(false);
}
private Session.StatusCallback statusCallback = new Session.StatusCallback() {
@Override
public void call(Session session, SessionState state, Exception exception) {
if (state.isOpened()) {
buttonsEnabled(true);
Log.d("FacebookSampleActivity", "Facebook session opened");
} else if (state.isClosed()) {
buttonsEnabled(false);
Log.d("FacebookSampleActivity", "Facebook session closed");
}
}
};
public void buttonsEnabled(boolean isEnabled) {
}
public boolean checkPermissions() {
Session s = Session.getActiveSession();
if (s != null) {
return s.getPermissions().contains("publish_actions");
} else
return false;
}
public void requestPermissions() {
Session s = Session.getActiveSession();
if (s != null)
s.requestNewPublishPermissions(new Session.NewPermissionsRequest(this, PERMISSIONS));
}
@Override
public void onResume() {
super.onResume();
uiHelper.onResume();
buttonsEnabled(Session.getActiveSession().isOpened());
}
@Override
public void onPause() {
super.onPause();
uiHelper.onPause();
}
@Override
public void onDestroy() {
super.onDestroy();
uiHelper.onDestroy();
}
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
uiHelper.onActivityResult(requestCode, resultCode, data);
}
@Override
public void onSaveInstanceState(Bundle savedState) {
super.onSaveInstanceState(savedState);
uiHelper.onSaveInstanceState(savedState);
}
}

Enjoy... :)

Comments

Popular Posts