¶ Login by Facebook
¶ Preparatory work
Configure in Facebook open platform (opens new window) and Authing Console (opens new window),See Preparing for FaceBook、FaceBook document (opens new window)。
This feature was added in android guard sdk 1.4.8 version.
¶ Integrate steps
¶ Step 1:Add dependency
- You need to open it first in your project your_app > Gradle Scripts > build.gradle (Project),Make sure the following repositories have been added to
buildscript { repositories {}}
:
mavenCentral()
- In the project, open your_app > Gradle Scripts > build.gradle (Module: app) and add the following execution statement to the
dependencies{}
section, adding the following dependencies:
implementation 'cn.authing:guard:+'
implementation 'com.facebook.android:facebook-login:latest.release'
The Guard compileOnly relies on gms, which allows apps to import on demand, preventing the Guard aar package from getting bigger as more third party logins are supported. Therefore, every time a third-party identity source is added, the App needs to manually add the dependency of the identity source.
¶ Step 2:Add resources and listings
Open
/app/res/values/strings.xml
.Add a string element named
facebook_app_id
、fb_login_protocol_scheme
andfacebook_client_token
,And set the app id (opens new window) and client token (opens new window). For example, if your application number is' 1234 'and the guest port order is' 56789', the code format looks like this:<string name="facebook_app_id">1234</string> <string name="fb_login_protocol_scheme">fb1234</string> <string name="facebook_client_token">56789</string>
Open
/app/manifest/AndroidManifest.xml
.Add the
meta-data
element to theapplication
element of the application number and customer port order:<application android:label="@string/app_name" ...> ... <meta-data android:name="com.facebook.sdk.ApplicationId" android:value="@string/facebook_app_id"/> <meta-data android:name="com.facebook.sdk.ClientToken" android:value="@string/facebook_client_token"/> ... </application>
In the
application
element, add an activity for Facebook, and add an activity and intent filter for the Chrome custom TAB:<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>
Add a
application
element to the list after theuses-permission
element:<uses-permission android:name="android.permission.INTERNET"/>
(optional) to disable ads code permissions (opens new window),Add the
uses-permission
element to the listing after theapplication
element:<uses-permission android:name="com.google.android.gms.permission.AD_ID" tools:node="remove"/>
¶ Step 3:Initialization
To initialize Guard Android SDK when the application starts:
// context is application or initial activity
// ”AUTHING_APP_ID“ is obtained from the Authing console
Authing.init(context, "AUTHING_APP_ID");
Authing.setAuthProtocol(Authing.AuthProtocol.EOIDC)
¶ Step 4:Use in different scenarios
¶ Use in different scenarios
Start the managed page where login authentication is required:
// this is the activity context
AuthFlow.start(this);
By following the preceding steps, you can quickly and easily configure the Authing management console to automatically have the FaceBook login function. The login entry is displayed in the social login button list on the built-in login interface of the Guard.
¶ Use the Facebook sign In button
If you use the Facebook login button we provide.
1. Add the following code to the layout file:
<cn.authing.guard.social.view.FaceBookLoginButton
android:id="@+id/btn_login"
android:background="@drawable/authing_button_background"
android:textColor="@color/white"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
2. Then handle the event in the code:
FaceBookLoginButton button = findViewById(R.id.btn_login);
button.setOnLoginListener(new AuthCallback<UserInfo>() {
@Override
public void call(int code, String message, UserInfo data) {
if (code == 200) {
// login success, data is user info.
} else {
// login fail
}
}
});
¶ Log in to the authorization class using Facebook
If you don't want to use our built-in buttons and want to implement the UI entirely yourself, you can call theFaceBook
class authorization function inside the button click event, which integrates the business logic to pull up the FaceBook authorization login:
FaceBook.getInstance().login(appContext, new AuthCallback<UserInfo>() {
@Override
public void call(int code, String message, UserInfo data) {
if (code == 200) {
// login success, data is user info.
} else {
// login fail
}
}
});
data
contains idToken
and user information (user name
, nickname
, name
, etc.).
Note: When using the FaceBook login button or the FaceBook login authorization class, you need to add the following code to the Activity's onActivityResult function:
@Override
protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
super.onActivityResult(requestCode, resultCode, data);
FaceBook.getInstance().onActivityResult(requestCode, resultCode, data);
}
¶ Log in to the API using Facebook
If you want to fully implement the Facebook login UI and obtain the authorization code logic yourself, after obtaining the authorization code, you can call the following API in exchange for user information:
public static void loginByFaceBook(String accessToken, @NotNull AuthCallback<UserInfo> callback)
param
accessToken
Facebook token
example
If you only need to get the user information (username
, nickname
, name
, etc.) and idToken
, call:
AuthClient.loginByFaceBook(accessToken, new AuthCallback<UserInfo>() {
@Override
public void call(int code, String message, UserInfo data) {
if (code == 200) {
// login success, data is user info, contains idToken.
} else {
// login fail
}
}
});
If you only need to get the user information (username
, nickname
, name
, etc.) and idToken
、accessToken
和 refreshToken
,call:
OIDCClient oidcClient = new OIDCClient();
oidcClient.loginByFaceBook(accessToken, new AuthCallback<UserInfo>() {
@Override
public void call(int code, String message, UserInfo data) {
if (code == 200) {
// ogin success, data is user info, contains idToken、accessToken and refreshToken.
} else {
// login fail
}
}
});