Skip to content

Instantly share code, notes, and snippets.

@vishalpawale
Last active February 22, 2019 19:26
Show Gist options
  • Save vishalpawale/5560725 to your computer and use it in GitHub Desktop.
Save vishalpawale/5560725 to your computer and use it in GitHub Desktop.
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);
}
}
@srinivas69
Copy link

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
Copy link

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
Copy link

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

@halim93
Copy link

halim93 commented Jan 10, 2017

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

@Dignesh1991
Copy link

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