Official: developers.facebook.com/docs/facebo…
Reference: www.jianshu.com/p/663e13104…
The ios side
To prepare
1. Log in to Facebook, register and configure the project, including info.plist configuration in Xcode
Developers.facebook.com/docs/facebo…
2. Double-click the project in Xcode and select “Info” → “URL Types” → fill in URL Schemes; URL Schemes comes from FB [APP_ID] in 1: Fb2014042434534780
3. Rely on
pod 'FBSDKLoginKit'
Copy the code
code
Enable Sign in
1.AppDelegate configuration using the Facebook SDK
AppDelegate.swift
func application(_ application: UIApplication.didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
// Facebook sign in
ApplicationDelegate.shared.application(application, didFinishLaunchingWithOptions: launchOptions)
return true
}
func application(_ app: UIApplication.open url: URL.options: [UIApplication.OpenURLOptionsKey : Any] = [:]) -> Bool {
//google sign in
var handled = GIDSignIn.sharedInstance().handle(url)
//Facebook sign in
if (!handled) {
handled = ApplicationDelegate.shared.application(
app,
open: url,
sourceApplication: options[UIApplication.OpenURLOptionsKey.sourceApplication] as? String,
annotation: options[UIApplication.OpenURLOptionsKey.annotation]
)
}
return handled
}
Copy the code
Call sign in
1. Request login through custom UIButton and receive callback
ViewController.swift
override func viewDidLoad(a) {
super.viewDidLoad()
button.addTarget(self, action: #selector(facebookSignIn()), for: UIControl.Event.touchUpInside)
}
@objc private func fbClick(a){
mFbLoginManager = LoginManager()
mFbLoginManager.logIn(permissions: ["public_profile"."email"], from: self) { (loginResult, error) in
if error ! = nil {
DLOG(self.LOGTAG."facebook login error")}else {
if let result = loginResult {
if result.isCancelled {
DLOG(self.LOGTAG."facebook login cancelled")}else {
DLOG(self.LOGTAG."facebook is login in")
Profile.loadCurrentProfile { (profile, error) in
if let profile = profile {
let name: String = profile.name ?? ""
if let current = AccessToken.current {
let userId: String = String(describing: current.userID)
let token: String = String(describing: current.tokenString)
// Tokens can be sent here to the background for validation
}
}
}
}
}
}
}
}
Copy the code
2. Call sign and delegate to listen for callbacks via FBLoginButton and LoginButtonDelegate
//NotificationCenter can listen for Facebook updates
Accesstoken. current Can get the current token
NotificationCenter.default.addObserver(forName: .AccessTokenDidChange, object: nil, queue: OperationQueue.main) { (notification) in
if let current = AccessToken.current, !token.isExpired {
let appId: String = String(describing: current.appID)
let userId: String = String(describing: current.userID)
let token: String = String(describing: current.tokenString)
DLOG(self.LOGTAG."facebook Observer appId: \(appId); userId: \(userId); token: \(token)")}}Copy the code
Back-end Token Authentication (Java)
Official: developers.facebook.com/docs/facebo…
public class FacebookIdentity {
String userId;
String clientId;//ios FacebookAppID
String identityToken;// token returned after ios sign in
}
public class FacebookTokenService {
private String accessUrl = "https://graph.facebook.com/v10.0/oauth/access_token";
public Result verifyToken(FacebookIdentity facebookIdentity){
try {
// Access password accessToken
String url = accessUrl + "? grant_type=fb_attenuate_token&client_id=" + facebookIdentity.getClientId() + "&fb_exchange_token=" + facebookIdentity.getIdentityToken();
String str = HttpClientUtils.doGet(url);
if (str == null || str.isEmpty()) {
return Result.fail("Facebook error verifying access password accessToken");
}
JSONObject data1 = JSONObject.parseObject(str);
System.err.println("fb Response1 =====>" + JSONObject.toJSONString(data1));
String access_token = data1.getString("access_token");
int expires_in = data1.getIntValue("expires_in");
if(access_token ! =null && expires_in > 0) {
return Result.success("Facebook Token valid");
}else {
return Result.fail("Facebook Token is invalid or expired"); }}catch (Exception ex) {
ex.printStackTrace();
return Result.fail("Facebook authentication request error");
}
}
}
Response
{
"access_token":"{session-info-access-token}"."token_type": "bearer"."expires_in": 5183944 //The number of seconds until the token expires
}
Copy the code