如需将 Google 登录功能集成到您的 Android 应用中,请配置 Google 登录功能,并在应用的布局中添加一个用于启动登录流程的按钮。
准备工作
配置 Google API 控制台项目并设置 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 登录您的应用。相应地更新界面,即隐藏登录按钮、启动主 activity 或任何适合您的应用的内容。
如果 GoogleSignIn.getLastSignedInAccount
返回 null
,则表示用户尚未使用 Google 登录您的应用。请更新您的界面以显示 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 令牌发送到后端服务器,并在服务器上验证该令牌。