This is the first article I participated in beginners’ introduction. Due to the project requirements of our company, we need to integrate the three-party login function of Facebook, so we hereby sort out and record the relevant process.
Facebook developers build apps
- Enter the following url developers.facebook.com/apps/ Facebook developer platform
- Log in to the Developer platform and click “Create App”
- Select the type of application to create
4. Enter an application name and click Create Application
5. Click “Add Product” in the left sidebar, or directly click the “Settings” button of Facebook login on the right of the picture to add the Facebook login function
Configure Facebook login for Android
1. Choose Quick Startup >Android
Gradle to add the following compiler statement to the dependencies{} section to introduce the FacebookSDK, and then click “continue”.
implementation 'com. Facebook. Android: facebook -- android SDK: [8, 9)'
Copy the code
3. Enter the package name and default activity class name, then click “Save” -> “Continue”
4. Add a key hash. Here I am the generated development key hash
Generate development key hashes: Mac OS
keytool -exportcert -alias androiddebugkey -keystore ~/.android/debug.keystore | openssl sha1 -binary | openssl base64
Copy the code
Windows
keytool -exportcert -alias androiddebugkey -keystore "C:\Users\USERNAME\.android\debug.keystore" | "PATH_TO_OPENSSL_LIBRARY\bin\openssl" sha1 -binary | "PATH_TO_OPENSSL_LIBRARY\bin\openssl" base64
Copy the code
Generate publish key hash:
keytool -exportcert -alias YOUR_RELEASE_KEY_ALIAS -keystore YOUR_RELEASE_KEY_PATH | openssl sha1 -binary | openssl base64
Copy the code
5. Select whether to enable single sign-on or not as required. I did not enable it here, and then click “Save” -> “Continue”.
6. Edit the AndroidManifest by directly copying the code generated by Facebook into strings. XML and androidmanifest.xml files
Now that you’re done with the Facebook platform, let’s start adding code to your own projects to make it work.
Add code to the project
1. Register callbacks to listen for returned login status
private lateinit var callbackManager: CallbackManager
callbackManager = CallbackManager.Factory.create()
// Callback registration
LoginManager.getInstance().registerCallback(
callbackManager,
object: FacebookCallback<LoginResult? > {override fun onSuccess(loginResult: LoginResult?). {
// App code
Timber.d("facebookLogin--onSuccess--")}override fun onCancel(a) {
// App code
Timber.d("facebookLogin--onCancel--")}override fun onError(exception: FacebookException) {
// App code
Timber.d("facebookLogin--onError--exception:%s", exception.printStackTrace())
}
})
Copy the code
2. Use the Login button provided by Facebook or a custom login button to use Facebook login button
<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" />
Copy the code
Custom login button, in the click event, add the following code to invoke the login function
LoginManager.getInstance().logIn(
this.null
)
Copy the code
3. Add it in onActivityResult
callbackManager.onActivityResult(requestCode, resultCode, data);
Copy the code
4. As required, you can add the judgment of Facebook login status on the APP startup page
LoginManager.getInstance()
.retrieveLoginStatus(
this.object : LoginStatusCallback {
override fun onCompleted(accessToken: AccessToken) {
Timber.d("--checkFacebookStatus LoginStatusCallback--, onCompleted:")}override fun onFailure(a) {
// If MainActivity is reached without the user being logged in,
// redirect to the Login Activity
Timber.d("--LoginStatusCallback-- onFailure")}override fun onError(exception: Exception) {
// Handle exception}})Copy the code
At this point, Facebook login is integrated. Now that you’re ready to test and verify, it’s important to note that in development mode, if you want to test with another Facebook account, you need to invite the corresponding Facebook account into the user’s identity.