Android-Google一键登陆/注册-刷新令牌
创始人
2024-08-19 18:30:24
0
  1. 首先在项目级别的 build.gradle 文件中添加以下代码:
dependencies {
    implementation 'com.google.android.gms:play-services-auth:19.0.0'
}
  1. 在需要使用 Google 一键登陆/注册的 Activity 中添加以下代码:
GoogleSignInOptions gso = new GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN)
        .requestEmail()
        .requestIdToken(getString(R.string.google_web_client_id))
        .build();
GoogleSignInClient mGoogleSignInClient = GoogleSignIn.getClient(this, gso);

其中,R.string.google_web_client_id 为 Google 分配的 Web 应用程序客户端 ID。 3. 在需要实现登陆/注册的地方添加以下代码:

Intent signInIntent = mGoogleSignInClient.getSignInIntent();
startActivityForResult(signInIntent, RC_SIGN_IN);

其中,RC_SIGN_IN 可以自定义请求编码。 4. 在 Activity 中添加 onActivityResult 方法,该方法用于处理 Google 登陆返回的结果:

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);

    // Result returned from launching the Intent from GoogleSignInClient.getSignInIntent(...);
    if (requestCode == RC_SIGN_IN) {
        Task task = GoogleSignIn.getSignedInAccountFromIntent(data);
        handleSignInResult(task);
    }
}

private void handleSignInResult(Task completedTask) {
    try {
        GoogleSignInAccount account = completedTask.getResult(ApiException.class);

        // Signed in successfully, show authenticated UI.
        updateUI(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());
        updateUI(null);
    }
}
  1. 在需要获取刷新令牌的地方添加以下代码:
private void getRefreshToken(GoogleSignInAccount account) {
    try {
        String refreshToken = account.getRefreshToken();
        Log.d(TAG, "Refresh Token: " + refreshToken);
    } catch (NullPointerException e) {
        Log.e(TAG, "Failed to get Refresh Token: " + e.getMessage());
    }
}

相关内容

热门资讯

一分钟带你了解!pokemmo... 一分钟带你了解!pokemmo手机脚本辅助器(辅助挂)切实是有挂(有挂助手)1、许多玩家不知道pok...
我来教大家!xpoker辅助控... 我来教大家!xpoker辅助控制(辅助挂)总是真的有挂(有挂存在)xpoker辅助控制能透视中分为三...
热点推荐!wepoker轻量版... 您好,wepoker轻量版透视系统这款游戏可以开挂的,确实是有挂的,需要了解加去威信【1367043...
程序员教你!wepoker透视... 程序员教你!wepoker透视辅助下载(辅助挂)切实真的有挂(有挂辅助)1、全新机制【wepoker...
玩家必备教程!wepoker开... 玩家必备教程!wepoker开辅助能查到吗(辅助挂)一直真的有挂(真的有挂)一、wepoker开辅助...
玩家必备教程!wepoker游... 玩家必备教程!wepoker游戏安装教程(辅助挂)确实真的是有挂(有挂规律)1、首先打开wepoke...
实测必看!wepoker透视脚... 实测必看!wepoker透视脚本免费(辅助挂)总是真的有挂(有挂秘笈)1.wepoker透视脚本免费...
教程攻略!竞技联盟辅助插件(辅... 教程攻略!竞技联盟辅助插件(辅助挂)好像是有挂(果真有挂)1、下载好竞技联盟辅助插件脚本下载之后点击...
技术分享!xpoker透视辅助... 技术分享!xpoker透视辅助(辅助挂)真是有挂(有挂存在)一、xpoker透视辅助可以开透视的定义...
玩家必看科普!wejoker辅... 玩家必看科普!wejoker辅助软件(辅助挂)都是有挂(真实有挂)1、进入到wejoker辅助软件是...