美文网首页
Android Google登录接入

Android Google登录接入

作者: 因为我的心 | 来源:发表于2023-03-23 14:19 被阅读0次

    一、接入准备

    1、手机必须有Google套件
    2、Google服务框架,Google Play商店,Google Play服务(使用su谷歌安装器下载安装)
    3、手机必须可以翻墙
    4、google开发者账号

    二、官网文档

    https://developers.google.com/identity/sign-in/android/start-integrating?hl=zh-cn

    必看三方文档:https://blog.csdn.net/wangchaojing/article/details/125680497

    三、接入步骤

    1、依赖google play服务

    implementation 'com.google.android.gms:play-services-auth:20.4.1'
    

    2、配置 Google API 控制台项目

    需要使用google开发者账号,创建项目,在项目中根据包名和SHA1值创建应用。

    控制台: https://console.cloud.google.com/apis/credentials?hl=zh-cn

    3、生成SHA1值

    图片.png

    注意:如果需要base64散列码,看下面

    将SHA1值复制到剪贴板,如CD:A1:EA:A3:5C:5C:68:FB:FA:0A:6B:E5:5A:72:64:DD:26:8D:44:84所示,然后打开http://tomeko.net/online_tools/hex_to_base64.php将SHA1值转换为base64。这是Facebook所要求的,获取生成的哈希值********************=,并将密钥哈希值复制到Facebook应用程序。

    在通过下面这个网站压缩:
    SHA1转化为BASE64.php

    图片.png

    4、代码

    1、 获取GoogleSignInClient 对象

        private final String TAG = getClass().getSimpleName();
    
        /**
         * google登录客户端对象
         */
        private GoogleSignInClient mGoogleSignInClient;
    
        private void initGoogleSignInClient() {
            // Configure sign-in to request the user's ID, email address, and basic
            // profile. ID and basic profile are included in DEFAULT_SIGN_IN.
            GoogleSignInOptions gso = new GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN)
                    //请求邮箱
                    .requestEmail()
                    //请求id token(web client id)
                    .requestIdToken("660809532020-g7r2qsmjkjv0ttmje013otrb97s0qtil.apps.googleusercontent.com")
                    .build();
            // Build a GoogleSignInClient with the options specified by gso.
            mGoogleSignInClient = GoogleSignIn.getClient(this, gso);
        }
    

    2、登录

    /**
         * google登录
         */
        private void login() {
            //使用GoogleSignIn.getLastSignedInAccount方法为当前登录的用户请求个人资料信息。
            GoogleSignInAccount account = GoogleSignIn.getLastSignedInAccount(this);
            //不存在则请求登录
            if (account == null) {
                Intent signInIntent = mGoogleSignInClient.getSignInIntent();
                mActivityLauncher.launch(signInIntent);
            } else {
                showAccount(account);
            }
        }
    
        private final ActivityResultLauncher<Intent> mActivityLauncher = registerForActivityResult(new ActivityResultContracts.StartActivityForResult(), result -> {
            if (result.getResultCode() == RESULT_OK) {
                //回调成功
                Task<GoogleSignInAccount> task = GoogleSignIn.getSignedInAccountFromIntent(result.getData());
                handleSignInResult(task);
            }
        });
    
        /**
         * 处理数据
         *
         * @param completedTask
         */
        private void handleSignInResult(Task<GoogleSignInAccount> completedTask) {
            try {
                GoogleSignInAccount account = completedTask.getResult(ApiException.class);
    
                // Signed in successfully, show authenticated UI.
                showAccount(account);
            } catch (ApiException e) {
                // The ApiException status code indicates the detailed failure reason.
                // Please refer to the GoogleSignInStatusCodes class reference for more information.
                Log.e(TAG, "signInResult:failed code=" + e.getStatusCode());
                Log.e(TAG, "signInResult:failed msg=" + e.getStatusMessage());
            }
        }
    
        /**
         * 展示账户信息
         *
         * @param account
         */
        private void showAccount(GoogleSignInAccount account) {
            Log.e(TAG, "signInResult:success name:" + account.getDisplayName());
            Log.e(TAG, "signInResult:success id:" + account.getId());
            Log.e(TAG, "signInResult:success email:" + account.getEmail());
            Log.e(TAG, "signInResult:success token:" + account.getIdToken());
    }
    

    3、 登出

    /**
         * 登出
         */
        private void logout() {
            mGoogleSignInClient.signOut()
                    .addOnCompleteListener(this, new OnCompleteListener<Void>() {
                        @Override
                        public void onComplete(@NonNull Task<Void> task) {
                            Log.e(TAG, "退出登录");
                        }
                    });
        }
    

    4、断开账户

    /**
         * 断开账户,撤销访问(如果用户删除其帐户,则必须删除您的应用程序从Google API获取的信息)。
         */
        private void revokeAccess() {
            mGoogleSignInClient.revokeAccess()
                    .addOnCompleteListener(this, new OnCompleteListener<Void>() {
                        @Override
                        public void onComplete(@NonNull Task<Void> task) {
                            Log.e(TAG, "断开账户");
                        }
                    });
        }
    

    参考:https://blog.csdn.net/liuxiaopang520/article/details/125508701

    相关文章

      网友评论

          本文标题:Android Google登录接入

          本文链接:https://www.haomeiwen.com/subject/sryirdtx.html