Created
February 19, 2015 12:00
-
-
Save eluleci/853157362f06d384136f to your computer and use it in GitHub Desktop.
Facebook Login Implementation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?xml version="1.0" encoding="utf-8"?> | |
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" | |
android:orientation="vertical" | |
android:layout_width="match_parent" | |
android:layout_height="match_parent"> | |
<Button | |
android:id="@+id/connect_with_facebook_btn" | |
android:layout_width="match_parent" | |
android:layout_height="wrap_content" | |
android:text="Connect with Facebook" | |
android:onClick="openFacebookLoginFragment" /> | |
</LinearLayout> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<application | |
android:icon=”@drawable/ic_launcher” | |
android:label=”@string/app_name”> | |
... | |
<!—- Facebook login --> | |
<activity android:name=”com.facebook.LoginActivity” /> | |
<meta-data | |
android:name=”com.facebook.sdk.ApplicationId” | |
android:value=”@string/facebook_app_id” /> | |
</application> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import android.os.Bundle; | |
import android.support.annotation.Nullable; | |
import android.support.v4.app.Fragment; | |
import android.util.Log; | |
import com.facebook.Request; | |
import com.facebook.Response; | |
import com.facebook.Session; | |
import com.facebook.SessionState; | |
import com.facebook.model.GraphUser; | |
public class FacebookLoginFragment extends Fragment implements Session.StatusCallback { | |
private static final String TAG = "FacebookLoginFragment"; | |
private FacebookAuthListener listener; | |
@Override | |
public void onActivityCreated(@Nullable Bundle savedInstanceState) { | |
super.onActivityCreated(savedInstanceState); | |
try { | |
listener = (FacebookAuthListener) getActivity(); | |
} catch (ClassCastException e) { | |
throw new RuntimeException("The activity that contains FacebookLoginFragment must implement FacebookLoginFragment.FacebookAuthListener"); | |
} | |
} | |
@Override | |
public void onCreate(Bundle savedInstanceState) { | |
super.onCreate(savedInstanceState); | |
Session.openActiveSession(getActivity(), true, this); | |
} | |
@Override | |
public void call(Session session, SessionState sessionState, Exception e) { | |
Log.d(TAG, "Session opened ? " + session.isOpened()); | |
if (session.isOpened() && listener.onFacebookAuthentication(session)) { | |
getUserData(session); | |
} | |
} | |
private void getUserData(final Session session) { | |
Request.newMeRequest(session, new Request.GraphUserCallback() { | |
@Override | |
public void onCompleted(GraphUser graphUser, Response response) { | |
Log.d(TAG, "User info is fetched from Facebook."); | |
listener.onFacebookUserFetched(session, graphUser); | |
} | |
}).executeAsync(); | |
} | |
public interface FacebookAuthListener { | |
/** | |
* Called when Facebook Authentication is successful. Returns a boolean whether fetching | |
* user data is required or not. If true, Facebook user data will be sent | |
* via 'onFacebookUserFetched()' method. | |
* | |
* @param session Facebook session | |
* @return | |
*/ | |
public boolean onFacebookAuthentication(Session session); | |
/** | |
* Called when user data is fetched. | |
* | |
* @param session Facebook session | |
* @param graphUser Facebook user object | |
*/ | |
public void onFacebookUserFetched(Session session, GraphUser graphUser); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import android.content.Intent; | |
import android.os.Bundle; | |
import android.support.v7.app.ActionBarActivity; | |
import android.util.Log; | |
import android.view.View; | |
import com.facebook.Session; | |
import com.facebook.model.GraphUser; | |
import com.miwi.R; | |
import com.miwi.fragment.FacebookLoginFragment; | |
public class LoginActivity extends ActionBarActivity implements FacebookLoginFragment.FacebookAuthListener { | |
private static final String TAG = "LoginActivity"; | |
@Override | |
protected void onCreate(Bundle savedInstanceState) { | |
super.onCreate(savedInstanceState); | |
setContentView(R.layout.activity_login); | |
} | |
@Override | |
protected void onActivityResult(int requestCode, int resultCode, Intent data) { | |
super.onActivityResult(requestCode, resultCode, data); | |
Session.getActiveSession().onActivityResult(this, requestCode, resultCode, data); | |
} | |
/** | |
* Opens FacebookLoginFragment. This method is bound from activity_login.xml. | |
* | |
* @param view | |
*/ | |
public void openFacebookLoginFragment(View view) { | |
getSupportFragmentManager() | |
.beginTransaction() | |
.add(android.R.id.content, new FacebookLoginFragment()) | |
.addToBackStack("FacebookLoginFragment") | |
.commit(); | |
} | |
@Override | |
public boolean onFacebookAuthentication(Session session) { | |
// returning true for fetching user info | |
return true; | |
} | |
@Override | |
public void onFacebookUserFetched(Session session, GraphUser graphUser) { | |
Log.d(TAG, "App ID: " + session.getApplicationId()); | |
Log.d(TAG, "Token: " + session.getAccessToken()); | |
Log.d(TAG, "User: " + graphUser.getName()); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment