เพิ่ม Google Sign-In ลงในแอป Android
กําหนดค่า Google Sign-In&โคลน;
// 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();
// Build a GoogleSignInClient with the options specified by gso. mGoogleSignInClient = GoogleSignIn.getClient(this, gso);
จากนั้นเมื่อคลิกปุ่มลงชื่อเข้าใช้ ให้เริ่มต้น Intent ลงชื่อเข้าใช้&โคลนและเครื่องหมายโคลอน
private void signIn() { Intent signInIntent = mGoogleSignInClient.getSignInIntent(); startActivityForResult(signInIntent, RC_SIGN_IN); }
ระบบจะแจ้งให้ผู้ใช้เลือกบัญชี Google ที่จะลงชื่อเข้าใช้ หากคุณขอใช้ขอบเขตนอกเหนือจาก profile
, email
และ openid
ระบบจะแจ้งให้ผู้ใช้ให้สิทธิ์เข้าถึงทรัพยากรที่ขอด้วย
สุดท้าย จัดการผลลัพธ์กิจกรรม&โคลนและโคลอน
@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); } }
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); } }

หากพร้อมผสานรวม Google Sign-In ในแอป Android แล้ว