Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Skeleton for login into Facebook
package com.sudosaints.android;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import com.facebook.Request;
import com.facebook.Response;
import com.facebook.Session;
import com.facebook.SessionState;
import com.facebook.model.GraphObject;
import com.facebook.model.GraphUser;
public class FbLoginActivity extends Activity {
private static List<String> permissions;
Session.StatusCallback statusCallback = new SessionStatusCallback();
ProgressDialog dialog;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.opening_screen_layout);
ImageView fbButton = (ImageView) findViewById(R.id.fbButton);
/***** FB Permissions *****/
permissions = new ArrayList<String>();
permissions.add("email");
/***** End FB Permissions *****/
fbButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Check if there is any Active Session, otherwise Open New Session
Session session = Session.getActiveSession();
if(!session.isOpened()) {
session.openForRead(new Session.OpenRequest(FbShareActivity.this).setCallback(statusCallback).setPermissions(permissions));
} else {
Session.openActiveSession(FbShareActivity.this, true, statusCallback);
}
}
});
Session session = Session.getActiveSession();
if(session == null) {
if(savedInstanceState != null) {
session = Session.restoreSession(this, null, statusCallback, savedInstanceState);
}
if(session == null) {
session = new Session(this);
}
Session.setActiveSession(session);
session.addCallback(statusCallback);
if(session.getState().equals(SessionState.CREATED_TOKEN_LOADED)) {
session.openForRead(new Session.OpenRequest(this).setCallback(statusCallback).setPermissions(permissions));
}
}
}
private class SessionStatusCallback implements Session.StatusCallback {
@Override
public void call(Session session, SessionState state, Exception exception) {
//Check if Session is Opened or not
processSessionStatus(session, state, exception);
}
}
public void processSessionStatus(Session session, SessionState state, Exception exception) {
if(session != null && session.isOpened()) {
if(session.getPermissions().contains("email")) {
//Show Progress Dialog
dialog = new ProgressDialog(FbShareActivity.this);
dialog.setMessage("Loggin in..");
dialog.show();
Request.executeMeRequestAsync(session, new Request.GraphUserCallback() {
@Override
public void onCompleted(GraphUser user, Response response) {
if (dialog!=null && dialog.isShowing()) {
dialog.dismiss();
}
if(user != null) {
Map<String, Object> responseMap = new HashMap<String, Object>();
GraphObject graphObject = response.getGraphObject();
responseMap = graphObject.asMap();
Log.i("FbLogin", "Response Map KeySet - " + responseMap.keySet());
// TODO : Get Email responseMap.get("email");
String fb_id = user.getId();
String email = null;
String name = (String) responseMap.get("name");
if (responseMap.get("email")!=null) {
email = responseMap.get("email").toString();
//TODO Login successfull Start your next activity
startActivity(new Intent(FbLoginActivity.this, HomeActivity.class));
}
else {
//Clear all session info & ask user to login again
Session session = Session.getActiveSession();
if(session != null) {
session.closeAndClearTokenInformation();
}
}
}
}
});
} else {
session.requestNewReadPermissions(new Session.NewPermissionsRequest(FbShareActivity.this, permissions));
}
}
}
/********** Activity Methods **********/
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
Log.d("FbLogin", "Result Code is - " + resultCode +"");
Session.getActiveSession().onActivityResult(FbShareActivity.this, requestCode, resultCode, data);
}
@Override
protected void onSaveInstanceState(Bundle outState) {
// TODO Save current session
super.onSaveInstanceState(outState);
Session session = Session.getActiveSession();
Session.saveSession(session, outState);
}
@Override
protected void onStart() {
// TODO Add status callback
super.onStart();
Session.getActiveSession().addCallback(statusCallback);
}
@Override
protected void onStop() {
// TODO Remove callback
super.onStop();
Session.getActiveSession().removeCallback(statusCallback);
}
}
@irq20xdfr

This comment has been minimized.

Copy link

@irq20xdfr irq20xdfr commented Aug 2, 2013

Great! it's just what i was looking for

@vishalpawale

This comment has been minimized.

Copy link
Owner Author

@vishalpawale vishalpawale commented Sep 22, 2013

Glad it helped :)

@kjhernandez

This comment has been minimized.

Copy link

@kjhernandez kjhernandez commented Sep 25, 2013

Is this also usable on a fragment? or do I have to place the events on the fragmentActivity?

@vishalpawale

This comment has been minimized.

Copy link
Owner Author

@vishalpawale vishalpawale commented Oct 25, 2013

Hi, Sorry for so late.
Yes you have to place events on Fragment Activity.You can pass callback to your fragment for initiating facebook login.

@bGorle

This comment has been minimized.

Copy link

@bGorle bGorle commented Feb 20, 2014

How to add the application Id

@kiyayi

This comment has been minimized.

Copy link

@kiyayi kiyayi commented Mar 27, 2014

How do i add the application id....I kindly need an urgent assistance. Tnx

@nichtemna

This comment has been minimized.

Copy link

@nichtemna nichtemna commented Apr 14, 2014

You can add app id in manifest.

@pimguilherme

This comment has been minimized.

Copy link

@pimguilherme pimguilherme commented Apr 29, 2014

Thank you!

@srinivas69

This comment has been minimized.

Copy link

@srinivas69 srinivas69 commented Nov 28, 2014

Hi,
I have implmented this code.
When I hit the facebook login button it is showing the login screen of facebook.
But when i close without login and try to hit the login button again it crashes saying "session: an attempt was made to open an already opened session."

@wnina

This comment has been minimized.

Copy link

@wnina wnina commented Feb 2, 2015

hi you have to use FbShareActivity.this not should be FbLoginActivity ???

if(!session.isOpened()) {
session.openForRead(new Session.OpenRequest(FbShareActivity.this).setCallback(statusCallback).setPermissions(permissions));
} else {
Session.openActiveSession(FbShareActivity.this, true, statusCallback);
}

thanks a lot !!!

@saurabhdacs

This comment has been minimized.

Copy link

@saurabhdacs saurabhdacs commented Sep 10, 2015

Hi , what is the FbShareActivity in this code.??

@halim93

This comment has been minimized.

Copy link

@halim93 halim93 commented Jan 10, 2017

hey how to import the library there are alot of error in the code !!!!

@Dignesh1991

This comment has been minimized.

Copy link

@Dignesh1991 Dignesh1991 commented Sep 18, 2017

which library i import????
please give me

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment