之前网上看到的关于google和facebook登录接入的介绍的很详细,却也比较复杂,今天我把我总结的贴出来,希望对大家有所帮助
一 Facebook登录
1.在facebook平台获取应用编号这个应该都会了,我直接该出API地址
https://developers.facebook.com/docs/android/getting-started
2 密钥散列的获取方式
hash key:
在你的Java jdk地址 目录下shift+鼠标右键打开cmd:
keytool -exportcert -alias xn -keystore "你的keystore地址" | "你的openssl地址" sha1 -binary | "你的openssl地址" base64
3 AndroidManifest.xml
你的应用编号
fb你的应用编号
4 封装代码
public class Facebook {
public CallbackManager callbackManager = CallbackManager.Factory.create();
private Activity activity;
private List permissions = Collections.emptyList();
public Facebook(final Activity activity) {
this.activity = activity;
LoginManager.getInstance().registerCallback(callbackManager, new FacebookCallback() {
@Override
public void onSuccess(LoginResult loginResult) {
// login success
if (loginResult == null) {
return;
}
AccessToken accessToken = loginResult.getAccessToken();
String userId = accessToken.getUserId();
//String token = accessToken.getToken();
Log.e("res", "facebook_res:" + accessToken.toString());
// 拿到userId和token,传给服务器校验
}
@Override
public void onCancel() {
WDQCPlatform.getInstance().doLogin(StatusCode.FAIL, null);
}
@Override
public void onError(FacebookException error) {
WDQCPlatform.getInstance().doLogin(StatusCode.FAIL, null);
}
});
permissions = Arrays.asList("email", "public_profile");
}
public void login() {
LoginManager.getInstance().logInWithReadPermissions(activity, permissions);
}
public void logout() {
LoginManager.getInstance().logOut();
}
}
5 执行代码
private Facebook faceBook;
private void login(){
if(faceBook == null){
faceBook = new Facebook(activity, this);
}
faceBook.login();
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (faceBook != null) {
faceBook.callbackManager.onActivityResult(requestCode, resultCode, data);
}
}
private void logout(){
if (faceBook != null) {
faceBook.logout();
}
}
二 google登录
1在google平台创建应用并下载google-services.json,我直接该出API地址
https://console.developers.google.com/apis/credentials
还有创建应用和下载google-services.json的地址
https://console.firebase.google.com/?hl=zh-cn
这个下载完就可以了
2 AndroidManifest.xml
8298000
3 封装代码
public class Google {
private FragmentActivity activity;
public GoogleSignInOptions gso;
public GoogleApiClient mGoogleApiClient;
public Google(FragmentActivity activity) {
this.activity = activity;
gso = new GoogleSignInOptions
.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN)
.requestEmail()
.requestId()
//.requestIdToken("504221091116-k75t0rf6forvi2e554no78f9s8dkf7uh.apps.googleusercontent.com")
.requestProfile()
.build();
mGoogleApiClient = new GoogleApiClient
.Builder(activity)
.enableAutoManage(activity, new OnConnectionFailedListener() {
@Override
public void onConnectionFailed(ConnectionResult result) {
// TODO Auto-generated method stub
}
})
.addApi(Auth.GOOGLE_SIGN_IN_API, gso)
.build();
}
public void login() {
Intent signInIntent = Auth.GoogleSignInApi.getSignInIntent(mGoogleApiClient);
activity.startActivityForResult(signInIntent, Constant.RC_GOOGLE);
}
public void logout() {
Auth.GoogleSignInApi.signOut(mGoogleApiClient).setResultCallback(new ResultCallback() {
@Override
public void onResult(Status status) {
// TODO Auto-generated method stub
if (status.isSuccess()) {
} else {
}
}
});
}
public String handleSignInResult(GoogleSignInResult result) {
String res = "";
if (result.isSuccess()) {
GoogleSignInAccount acct = result.getSignInAccount();
res = "登录成功--" + "用户名为:" + acct.getDisplayName() + " 邮箱为:" + acct.getEmail() + " token为:" + acct.getIdToken()
+ " 头像地址为:" + acct.getPhotoUrl() + " Id为:" + acct.getId() + " GrantedScopes为:"
+ acct.getGrantedScopes();
Log.e("res", "google_res:" + res);
// 拿到userId和token,传给服务器校验
return res;
}
}
4 执行代码
private Google googleLogin;
private void login(){
if(googleLogin == null){
googleLogin = new Google(activity, this);
}
googleLogin.login();
}
private void logout(){
if (googleLogin != null) {
googleLogin.logout();
}
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == Constant.RC_GOOGLE && googleLogin != null) {
GoogleSignInResult result = Auth.GoogleSignInApi.getSignInResultFromIntent(data);
googleLogin.handleSignInResult(result);
}
}
好,到此结束!!!