欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

Google登录集成(Android)

程序员文章站 2024-01-09 22:24:34
...

测试google登录的条件:
1、手机具备*能力
2、手机为google手机或者安装有google服务
3、应用必须被正确的签名文件签名
参考google开发文档:
https://developers.google.com/identity/sign-in/android/sign-in
集成步骤:
第一步:注册登录google移动平台
第二步:添加你的超赞项目,设置地区
Google登录集成(Android)
第三步:创建项目完成后,选择集成平台
Google登录集成(Android)
第四步:选择Android,设置包名、sha1等信息
Google登录集成(Android)
第五步:注册应用,然后跳转的页面中切记要下载json文件,并将json文件放到主module根目录下
Google登录集成(Android)
第六步:
项目级 build.gradle(/build.gradle):
buildscript {
dependencies {
// Add this line
classpath ‘com.google.gms:google-services:3.2.0’
}
}

应用级 build.gradle:
dependencies {
// Add this line
compile ‘com.google.firebase:firebase-core:12.0.1’
}

// Add to the bottom of the file
apply plugin: ‘com.google.gms.google-services’
第七步:环境配置完成,Sync Now…
第八步:sdk 需要下载google_play_services
Google登录集成(Android)
第九步:定义初始化回调接口,定义登录回调接口:

public interface GoogleInitListener {
    void initFinish(GoogleSignInAccount account,GoogleSignInClient client );
}
public interface GoogleLoginListener {
    void loginSuccess(GoogleSignInAccount account);
    void LoginFailed(String msg);
}

第十步:点击google登录按钮开始进行初始化:

String google_app_id = "your app id";
public void gooleAuthInit( Activity activity,GoogleInitListener googleInitListener){
    GoogleSignInOptions gso = new GoogleSignInOptions
                    .Builder(GoogleSignInOptions.DEFAULT_SIGN_IN)
                    .requestIdToken(googleId)
                    .requestEmail()
                    .build();

    GoogleSignInClient mGoogleSignInClient = GoogleSignIn.getClient(activity, gso);
    //如果用戶已经登录返回account对象,没有登录 返回null
    GoogleSignInAccount account = GoogleSignIn.getLastSignedInAccount(activity);
    if(account==null){
        //初始化完成,检查到google账户没有登录
        googleInitListener.initFinish(null,mGoogleSignInClient);
    }else{
        //初始化完成,检查到google账户已经登录,返回账号信息
        googleInitListener.initFinish(account,mGoogleSignInClient);
    }
}

第十一步:收到初始化回调对象时,调用登录:

private GoogleLoginListener mGoogleAuthListener;
//打开google登录页
public void gooleAuthLogin(Activity activity,  GoogleSignInClient mGoogleSignInClient,GoogleLoginListener googleAuthListener){
        mGoogleAuthListener = googleAuthListener;
        Intent signInIntent = mGoogleSignInClient.getSignInIntent();
        activity.startActivityForResult(signInIntent,RC_SIGN_IN);
}
//登录结果返回
public void onActivityResult(int requestCode, int resultCode, Intent data) {
    // Result returned from launching the Intent from GoogleSignInClient.getSignInIntent(...);
    if (requestCode == RC_SIGN_IN) {
        // The Task returned from this call is always completed, no need to attach
        Task<GoogleSignInAccount> task = GoogleSignIn.getSignedInAccountFromIntent(data);
        handleSignInResult(task);
    }
}
//处理登录结果,并回调登录结果
private void handleSignInResult(Task<GoogleSignInAccount> completedTask) {
    try {
        GoogleSignInAccount account = completedTask.getResult(ApiException.class);
        // Signed in successfully, show authenticated UI.
        mGoogleAuthListener.loginSuccess(account);
    } catch (ApiException e) {
        // The ApiException status code indicates the detailed failure reason.
        // Please refer to the GoogleSignInStatusCodes class reference for more information.
        Log.w(TAG, "signInResult:failed code=" + e.getStatusCode());
        Log.d(TAG, e.getMessage());
        e.printStackTrace();
        mGoogleAuthListener.LoginFailed(e.getStatusCode()+","+e.getMessage());
    }
}

登录流程到此结束,欢迎大家留言批评指正,谢谢!

上一篇: php画图函数画不出图来,

下一篇: