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());
    }
}

相关内容

热门资讯

透视讲解!wejoker辅助机... 透视讲解!wejoker辅助机器人(透视)其实是真的有挂(详细辅助解说技巧);1、透视辅助简单,软件...
透视有挂!wpk辅助(透视)确... 透视有挂!wpk辅助(透视)确实真的是有挂(详细辅助微扑克教程);进入游戏-大厅左侧-新手福利-激活...
透视透视!wepoker有透视... 透视透视!wepoker有透视底牌吗(透视)都是存在有挂(详细辅助技巧教程)1、打开软件启动之后找到...
透视脚本!aapoker脚本怎... 透视脚本!aapoker脚本怎么用(透视)一贯是有挂(详细辅助规律教程)1、超多福利:超高返利,海量...
透视真的!wepokerh5破... 透视真的!wepokerh5破解(透视)本来有挂(详细辅助系统教程)1、构建自己的辅助插件;2、选择...
透视透视!wepoker有插件... 透视透视!wepoker有插件吗(透视)好像有挂(详细辅助介绍教程)1、玩家可以在软件透明挂俱乐部对...
透视肯定!有没有人wepoke... 透视肯定!有没有人wepoker(透视)好像真的有挂(详细辅助2025新版教程)辅助器中分为三种模型...
透视攻略!wepokerplu... 透视攻略!wepokerplus透视脚本免费(透视)确实真的有挂(详细辅助必胜教程);透视攻略!we...
透视软件!wepoker模拟器... 透视软件!wepoker模拟器哪个好用(透视)总是有挂(详细辅助攻略教程)辅助器中分为三种模型:软件...
透视app!wepoker免费... 透视app!wepoker免费透视(透视)一直有挂(详细辅助科技教程)一、软件透明挂的定义与意义1、...