|
|
@ -10,7 +10,7 @@ import 'package:get/get.dart'; |
|
|
import 'package:crypto/crypto.dart'; |
|
|
import 'package:crypto/crypto.dart'; |
|
|
|
|
|
|
|
|
class Facebook { |
|
|
class Facebook { |
|
|
String token = ''; |
|
|
String? token = ''; |
|
|
|
|
|
|
|
|
String name = ''; |
|
|
String name = ''; |
|
|
String email = ''; |
|
|
String email = ''; |
|
|
@ -31,79 +31,101 @@ class Facebook { |
|
|
return digest.toString(); |
|
|
return digest.toString(); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
// FaceBook登录 |
|
|
|
|
|
// 修改后的 signInWithFacebook |
|
|
|
|
|
Future<bool> signInWithFacebook() async { |
|
|
Future<bool> signInWithFacebook() async { |
|
|
try { |
|
|
try { |
|
|
final rawNonce = generateNonce(); |
|
|
UserCredential userCredential = await loginFacebook(); |
|
|
final nonce = sha256ofString(rawNonce); |
|
|
//UserCredential userCredential = await loginGoogle(); |
|
|
final LoginResult loginResult = await FacebookAuth.instance |
|
|
token = await userCredential.user?.getIdToken(); |
|
|
.login( |
|
|
print('facebook响应数据userCredential: $token'); |
|
|
loginTracking: LoginTracking.enabled, |
|
|
if (token != "") { |
|
|
nonce: nonce, |
|
|
return true; |
|
|
) |
|
|
|
|
|
.catchError((onError) { |
|
|
|
|
|
if (kDebugMode) { |
|
|
|
|
|
//print(onError); |
|
|
|
|
|
} |
|
|
|
|
|
throw Exception(onError.message); |
|
|
|
|
|
}); |
|
|
|
|
|
if (loginResult.accessToken == null) { |
|
|
|
|
|
throw Exception(loginResult.message); |
|
|
|
|
|
} |
|
|
|
|
|
// Create a credential from the access token |
|
|
|
|
|
OAuthCredential facebookAuthCredential; |
|
|
|
|
|
|
|
|
|
|
|
print("tokenType${loginResult.accessToken!.type}"); |
|
|
|
|
|
|
|
|
|
|
|
if (Platform.isIOS) { |
|
|
|
|
|
// Create a credential from the access token |
|
|
|
|
|
switch (loginResult.accessToken!.type) { |
|
|
|
|
|
case AccessTokenType.classic: |
|
|
|
|
|
final classicToken = loginResult.accessToken as ClassicToken; |
|
|
|
|
|
token = classicToken.tokenString; |
|
|
|
|
|
print('Token 字符串: ${classicToken.tokenString}'); |
|
|
|
|
|
print('classicToken值: ${classicToken}'); |
|
|
|
|
|
facebookAuthCredential = FacebookAuthProvider.credential( |
|
|
|
|
|
classicToken.authenticationToken!, |
|
|
|
|
|
); |
|
|
|
|
|
break; |
|
|
|
|
|
case AccessTokenType.limited: |
|
|
|
|
|
final limitedToken = loginResult.accessToken as LimitedToken; |
|
|
|
|
|
token = limitedToken.tokenString; |
|
|
|
|
|
print('Token 字符串: ${limitedToken.tokenString}'); |
|
|
|
|
|
print('LimitedToken值: ${limitedToken}'); |
|
|
|
|
|
facebookAuthCredential = OAuthCredential( |
|
|
|
|
|
providerId: 'facebook.com', |
|
|
|
|
|
signInMethod: 'oauth', |
|
|
|
|
|
idToken: limitedToken.tokenString, |
|
|
|
|
|
rawNonce: rawNonce, |
|
|
|
|
|
); |
|
|
|
|
|
break; |
|
|
|
|
|
} |
|
|
|
|
|
} else { |
|
|
} else { |
|
|
facebookAuthCredential = FacebookAuthProvider.credential( |
|
|
return false; |
|
|
loginResult.accessToken!.tokenString, |
|
|
|
|
|
); |
|
|
|
|
|
token = loginResult.accessToken!.tokenString; |
|
|
|
|
|
} |
|
|
} |
|
|
print('facebookAuthCredential值: ${facebookAuthCredential}'); |
|
|
} catch (e) { |
|
|
if (facebookAuthCredential == null) { |
|
|
print(e); |
|
|
throw Exception("Facebook Login Fail."); |
|
|
return false; |
|
|
|
|
|
} |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
// FaceBook登录 |
|
|
|
|
|
// 修改后的 signInWithFacebook |
|
|
|
|
|
Future<UserCredential> loginFacebook() async { |
|
|
|
|
|
//try { |
|
|
|
|
|
final rawNonce = generateNonce(); |
|
|
|
|
|
final nonce = sha256ofString(rawNonce); |
|
|
|
|
|
final LoginResult loginResult = await FacebookAuth.instance |
|
|
|
|
|
.login( |
|
|
|
|
|
loginTracking: LoginTracking.enabled, |
|
|
|
|
|
nonce: nonce, |
|
|
|
|
|
) |
|
|
|
|
|
.catchError((onError) { |
|
|
|
|
|
if (kDebugMode) { |
|
|
|
|
|
//print(onError); |
|
|
} |
|
|
} |
|
|
final userData = await FacebookAuth.instance.getUserData(); |
|
|
throw Exception(onError.message); |
|
|
name = userData["name"].toString(); |
|
|
}); |
|
|
|
|
|
if (loginResult.accessToken == null) { |
|
|
|
|
|
throw Exception(loginResult.message); |
|
|
|
|
|
} |
|
|
|
|
|
// Create a credential from the access token |
|
|
|
|
|
OAuthCredential facebookAuthCredential; |
|
|
|
|
|
|
|
|
email = userData["email"].toString(); |
|
|
print("tokenType${loginResult.accessToken!.type}"); |
|
|
pictureurl = userData["picture"]["data"]["url"].toString(); |
|
|
|
|
|
id = userData["id"].toString(); |
|
|
|
|
|
print("user login facebook ${email}${pictureurl}${name}${id}"); |
|
|
|
|
|
|
|
|
|
|
|
return true; |
|
|
if (Platform.isIOS) { |
|
|
} catch (e) { |
|
|
// Create a credential from the access token |
|
|
print("Facebook 登录异常:$e"); |
|
|
switch (loginResult.accessToken!.type) { |
|
|
return false; |
|
|
case AccessTokenType.classic: |
|
|
|
|
|
final classicToken = loginResult.accessToken as ClassicToken; |
|
|
|
|
|
// token = classicToken.tokenString; |
|
|
|
|
|
print('Token 字符串: ${classicToken.tokenString}'); |
|
|
|
|
|
print('classicToken值: ${classicToken}'); |
|
|
|
|
|
facebookAuthCredential = FacebookAuthProvider.credential( |
|
|
|
|
|
classicToken.authenticationToken!, |
|
|
|
|
|
); |
|
|
|
|
|
break; |
|
|
|
|
|
case AccessTokenType.limited: |
|
|
|
|
|
final limitedToken = loginResult.accessToken as LimitedToken; |
|
|
|
|
|
//token = limitedToken.tokenString; |
|
|
|
|
|
print('Token 字符串: ${limitedToken.tokenString}'); |
|
|
|
|
|
print('LimitedToken值: ${limitedToken}'); |
|
|
|
|
|
facebookAuthCredential = OAuthCredential( |
|
|
|
|
|
providerId: 'facebook.com', |
|
|
|
|
|
signInMethod: 'oauth', |
|
|
|
|
|
idToken: limitedToken.tokenString, |
|
|
|
|
|
rawNonce: rawNonce, |
|
|
|
|
|
); |
|
|
|
|
|
break; |
|
|
|
|
|
} |
|
|
|
|
|
} else { |
|
|
|
|
|
facebookAuthCredential = FacebookAuthProvider.credential( |
|
|
|
|
|
loginResult.accessToken!.tokenString, |
|
|
|
|
|
); |
|
|
|
|
|
//token = loginResult.accessToken!.tokenString; |
|
|
|
|
|
} |
|
|
|
|
|
print('facebookAuthCredential值: ${facebookAuthCredential}'); |
|
|
|
|
|
if (facebookAuthCredential == null) { |
|
|
|
|
|
throw Exception("Facebook Login Fail."); |
|
|
} |
|
|
} |
|
|
|
|
|
final userData = await FacebookAuth.instance.getUserData(); |
|
|
|
|
|
name = userData["name"].toString(); |
|
|
|
|
|
|
|
|
|
|
|
email = userData["email"].toString(); |
|
|
|
|
|
pictureurl = userData["picture"]["data"]["url"].toString(); |
|
|
|
|
|
id = userData["id"].toString(); |
|
|
|
|
|
print("user login facebook ${email}${pictureurl}${name}${id}"); |
|
|
|
|
|
return FirebaseAuth.instance.signInWithCredential(facebookAuthCredential); |
|
|
|
|
|
|
|
|
|
|
|
// final User? user = userCredential.user; // 获取用户信息 |
|
|
|
|
|
// token = await userCredential.user?.getIdToken(); |
|
|
|
|
|
// print("irebaseAuth.instance.signInWithCredential ${token}"); |
|
|
|
|
|
|
|
|
|
|
|
// return true; |
|
|
|
|
|
// } catch (e) { |
|
|
|
|
|
// print("Facebook 登录异常:$e"); |
|
|
|
|
|
// return false; |
|
|
|
|
|
// } |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
// 退出Google登录 |
|
|
// 退出Google登录 |
|
|
|