Last active
August 29, 2015 14:26
-
-
Save bkawakami/d925549e119bdeb995b1 to your computer and use it in GitHub Desktop.
Repo and code for Android Facebook auth
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
compile 'com.facebook.android:facebook-android-sdk:4.1.0' |
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
<ScrollView | |
xmlns:android="http://schemas.android.com/apk/res/android" | |
xmlns:tools="http://schemas.android.com/tools" | |
xmlns:facebook="http://schemas.android.com/apk/res-auto" | |
android:layout_width="match_parent" | |
android:layout_height="match_parent"> | |
<LinearLayout | |
android:orientation="vertical" | |
android:layout_width="match_parent" | |
android:layout_height="match_parent"> | |
<com.facebook.login.widget.LoginButton | |
android:id="@+id/login_button" | |
android:layout_width="wrap_content" | |
android:layout_height="wrap_content" | |
android:layout_gravity="center_horizontal" | |
android:layout_marginTop="30dp" | |
android:layout_marginBottom="30dp" /> | |
</LinearLayout> | |
</ScrollView> |
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
public fun facebookAuth(fragment: Fragment) { | |
val permissions = Arrays.asList("email", "public_profile", "user_events") | |
val loginButton = rootView!!.findViewById(R.id.login_button) as LoginButton | |
loginButton.setReadPermissions(permissions) | |
// If using in a fragment | |
loginButton.setFragment(this) | |
// Other app specific specialization | |
loginButton.setOnClickListener(object : View.OnClickListener { | |
override fun onClick(v: View) { | |
object : AccessTokenTracker() { | |
override fun onCurrentAccessTokenChanged(oldAccessToken: AccessToken?, currentAccessToken: AccessToken?) { | |
if (currentAccessToken == null) { | |
// Here you put when do Logout | |
} | |
} | |
} | |
} | |
}) | |
// Callback registration | |
loginButton.registerCallback(callbackManager, object : FacebookCallback<LoginResult> { | |
override fun onSuccess(loginResult: LoginResult) { | |
// App code | |
val accessToken = loginResult.getAccessToken().getToken() | |
GraphRequest.newMeRequest(loginResult.getAccessToken(), object : GraphRequest.GraphJSONObjectCallback { | |
override fun onCompleted(user: JSONObject?, response: GraphResponse) { | |
if (user != null) { | |
/** | |
* How to get user login info | |
* user.optString("id") | |
* user.optString("name") | |
* user.optString("email") | |
*/ | |
} | |
} | |
}).executeAsync() | |
} | |
override fun onCancel() { | |
} | |
override fun onError(exception: FacebookException) { | |
} | |
}) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment