要将 Google 登录集成到您的 Android 应用中,请配置 Google 登录并在应用的布局中添加一个启动登录流程的按钮。
准备工作
配置 Google API Console 项目并设置您的 Android Studio 项目。
配置 Google 登录和 GoogleSignInClient 对象
在登录 Activity 的
onCreate方法中,配置 Google 登录以请求您的应用所需的用户数据。例如,要配置 Google 登录以请求用户的 ID 和基本个人资料信息,请使用DEFAULT_SIGN_IN参数创建GoogleSignInOptions对象。要同时请求用户的电子邮件地址,请使用requestEmail选项创建GoogleSignInOptions对象。// 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() .build();
如果您需要请求其他范围以访问 Google API,请使用
requestScopes指定它们。为了提供最佳用户体验,登录时仅请求应用正常运行所需的最低限度的范围。仅在需要时才请求其他范围,以便用户在执行操作的上下文中看到同意屏幕。请参阅请求其他范围。然后,同样在登录 Activity 的
onCreate方法中,使用您指定的选项创建GoogleSignInClient对象。// Build a GoogleSignInClient with the options specified by gso. mGoogleSignInClient = GoogleSignIn.getClient(this, gso);
检查是否存在已登录用户
在 Activity 的 onStart 方法中,检查用户是否已使用 Google 登录到您的应用。
// Check for existing Google Sign In account, if the user is already signed in // the GoogleSignInAccount will be non-null. GoogleSignInAccount account = GoogleSignIn.getLastSignedInAccount(this); updateUI(account);
如果 GoogleSignIn.getLastSignedInAccount 返回 GoogleSignInAccount 对象(而非 null),则用户已使用 Google 登录到您的应用。相应地更新您的 UI — 例如,隐藏登录按钮、启动主 Activity 或执行适合您应用的任何操作。
如果 GoogleSignIn.getLastSignedInAccount 返回 null,则用户尚未通过 Google 登录到您的应用。更新您的 UI 以显示 Google 登录按钮。
将 Google 登录按钮添加到您的应用
在应用的布局中添加 SignInButton<com.google.android.gms.common.SignInButton android:id="@+id/sign_in_button" android:layout_width="wrap_content" android:layout_height="wrap_content" />可选:如果您使用的是默认登录按钮图形,而不是提供自己的登录按钮素材资源,则可以使用
setSize方法自定义按钮大小。// Set the dimensions of the sign-in button. SignInButton signInButton = findViewById(R.id.sign_in_button); signInButton.setSize(SignInButton.SIZE_STANDARD);
在 Android Activity 中(例如,在
onCreate方法中),注册按钮的OnClickListener,以便在用户点击时登录。findViewById(R.id.sign_in_button).setOnClickListener(this);
启动登录流程
在 Activity 的 onClick方法中,通过使用getSignInIntent方法创建登录 intent,并使用startActivityForResult启动该 intent,处理登录按钮的点击事件。@Override public void onClick(View v) { switch (v.getId()) { case R.id.sign_in_button: signIn(); break; // ... } }private void signIn() { Intent signInIntent = mGoogleSignInClient.getSignInIntent(); startActivityForResult(signInIntent, RC_SIGN_IN); }启动该 intent 会提示用户选择一个 Google 帐号进行登录。如果您请求了
profile、email和openid之外的范围,系统还会提示用户授予对所请求资源的访问权限。用户登录后,您可以在 Activity 的
onActivityResult方法中获取用户的GoogleSignInAccount对象。@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) { // The Task returned from this call is always completed, no need to attach // a listener. Task<GoogleSignInAccount> task = GoogleSignIn.getSignedInAccountFromIntent(data); handleSignInResult(task); } }
GoogleSignInAccount对象包含有关已登录用户的信息,例如用户名。private void handleSignInResult(Task<GoogleSignInAccount> 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); } }
您还可以使用
getEmail获取用户电子邮件地址,使用getId获取用户的 Google ID(供客户端使用),以及使用getIdToken获取用户的 ID 令牌。如果您需要将当前已登录用户传递给后端服务器,请将 ID 令牌发送到您的后端服务器并在服务器上验证该令牌。