Android Google登录接入

一、接入准备

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

二、官网文档

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

三、接入步骤

  1. 依赖google play服务
implementation 'com.google.android.gms:play-services-auth:20.2.0'
  1. 配置 Google API 控制台项目

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

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

  2. 代码

    3.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);
    }

3.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.3 登出

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

3.4 断开账户

/**
     * 断开账户,撤销访问(如果用户删除其帐户,则必须删除您的应用程序从Google API获取的信息)。
     */
    private void revokeAccess() {
        mGoogleSignInClient.revokeAccess()
                .addOnCompleteListener(this, new OnCompleteListener<Void>() {
                    @Override
                    public void onComplete(@NonNull Task<Void> task) {
                        Log.e(TAG, "断开账户");
                    }
                });
    }
  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值