Official documents
Facebook logs in to Android official documents
Create application
First go Facebook Developer Platform Register for a developer account where you need to go over the wall and add your own application in the background
SDK integration
There are two ways to integrate SDK
- Maven mode
- In your project, open your_app > Gradle Scripts > build. gradle (Project) to ensure that the following repositories are added to buildscript {repositories {}}
jcenter()
- In your project, open your_app > Gradle Scripts > build. gradle (Module: app) and add the following execution statements to the dependencies {} section to rely on the latest version of Facebook to log in to SDK:
implementation 'com.facebook.android:facebook-login:[4,5)'
-
Jar import mode
Download Facebook's latest SDK jar package and place it in the libs folder of the project.
The SDK download address is: Facebook SDK Download Address
Editing resources and lists
- Open the / app/res/values/strings.xml file
<string name="facebook_app_id">15477621189....</string>
<string name="fb_login_protocol_scheme">fb15477621189....</string>
- Open the / APP / manifest / Android Manifest. XML file
- add permission
<uses-permission android:name="android.permission.INTERNET"/>
- Add the following meta-data elements and the corresponding Activity to the application element
<meta-data android:name="com.facebook.sdk.ApplicationId"
android:value="@string/facebook_app_id"/>
<activity android:name="com.facebook.FacebookActivity"
android:configChanges=
"keyboard|keyboardHidden|screenLayout|screenSize|orientation"
android:label="@string/app_name" />
<activity
android:name="com.facebook.CustomTabActivity"
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.VIEW" />
<category android:name="android.intent.category.DEFAULT" />
<category android:name="android.intent.category.BROWSABLE" />
<data android:scheme="@string/fb_login_protocol_scheme" />
</intent-filter>
</activity>
Fill in the package name of APP
Fill in the background of Facebook control
Developing and publishing key hashes for applications
First, three tools are needed:
- Keytool: The default in JDK already exists
- windows openssl Library
- Keystore: debug.keystore and keystore for officially launched applications
Secondly, using the command line cmd to generate debugging and formal keystore key hash, cmd channel JDK directory, enter a command, pay attention to adjusting their own paths, debug.keystore password default is not available, just return directly.
- Debugging version secret key hash
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
- Formal version secret key hash
keytool -exportcert -alias YOUR_RELEASE_KEY_ALIAS -keystore YOUR_RELEASE_KEY_PATH | openssl sha1 -binary | openssl base64
The generated secret key hash is a string of 28 characters, similar to the following
QvoiWD1LQEfDIrvczaq31WCJZjX=
API usage of SDK
- Initialized login callback for SDK, called in onCreate()
callbackManager = CallbackManager.Factory.create();
LoginManager.getInstance().registerCallback(callbackManager, new FacebookCallback<LoginResult>() {
@Override
public void onSuccess(LoginResult loginResult) {
Log.e(TAG, "Login successfully: " + loginResult.getAccessToken().getToken());
loginResult.getAccessToken().getApplicationId();
loginResult.getAccessToken().getUserId();
}
@Override
public void onCancel() {
Log.e(TAG, "Login cancelled");
}
@Override
public void onError(FacebookException error) {
Log.e(TAG, "Login error");
}
});
- Call the login interface where you need to call the login
LoginManager.getInstance().logInWithReadPermissions(MainActivity.this, Arrays.asList("public_profile"));
- Call callbackManager.onActivityResult in the onActivityResult method and pass the login result to LoginManager through the callbackManager
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
callbackManager.onActivityResult(requestCode, resultCode, data);
super.onActivityResult(requestCode, resultCode, data);
}
Such a simple Facebook login client API has been completed, if you need server-side validation, the way of validation is different, you can refer to it. Access password
Application testing
- Testing Cell Phone: Need to Turn over Wall
- You need a google play service that can be downloaded from major app stores: Google Installer
- Download Facebook app from google play Store
- Install debug on behalf of buyer into mobile phone running test
Matters needing attention
- Configure your debug.keystore hash key to the Facebook control background, otherwise the debug app will be unavailable to the mobile phone.
- Note that your application parameters are properly configured to: / app/res/values/strings.xml
- If you configure the package name in the Facebook control background, make sure that your application's package name is the same as the Facebook configuration.