หากต้องการผสานรวม Google Sign-In เข้ากับแอป Android ให้กําหนดค่า Google Sign-In และเพิ่มปุ่มลงในเลย์เอาต์ของแอปที่เริ่มขั้นตอนการลงชื่อเข้าใช้
ข้อควรทราบก่อนที่จะเริ่มต้น
กําหนดค่าโครงการคอนโซล Google API และตั้งค่าโครงการ Android Studio
กําหนดค่า Google Sign-In และออบเจ็กต์ GoogleSignInClient
ในเมธอด
onCreate
ของกิจกรรมการลงชื่อเข้าใช้ ให้กําหนดค่า Google Sign-In เพื่อขอข้อมูลผู้ใช้ที่จําเป็นสําหรับแอป เช่น หากต้องการกําหนดค่า Google Sign-In ให้ขอรหัสผู้ใช้และข้อมูลโปรไฟล์พื้นฐาน ให้สร้างออบเจ็กต์GoogleSignInOptions
ที่มีพารามิเตอร์DEFAULT_SIGN_IN
หากต้องการขอที่อยู่อีเมลของผู้ใช้ด้วย ให้สร้างออบเจ็กต์GoogleSignInOptions
ที่มีตัวเลือกrequestEmail
// 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
เพื่อประสบการณ์ที่ดีที่สุดของผู้ใช้ ในการลงชื่อเข้าใช้ ให้ขอเฉพาะขอบเขตที่จําเป็นสําหรับแอปของคุณเพื่อให้ทํางานได้น้อยที่สุด ขอขอบเขตเพิ่มเติมเมื่อคุณต้องการเท่านั้น เพื่อให้ผู้ใช้เห็นหน้าจอคํายินยอมในบริบทของการดําเนินการที่ทํา ดูการขอขอบเขตเพิ่มเติมจากนั้น ให้สร้างเมธอด
GoogleSignInClient
ด้วยตัวเลือกที่คุณระบุในเมธอดonCreate
ของกิจกรรมการลงชื่อเข้าใช้// Build a GoogleSignInClient with the options specified by gso. mGoogleSignInClient = GoogleSignIn.getClient(this, gso);
ตรวจหาผู้ใช้ที่ลงชื่อเข้าใช้อยู่
ในเมธอด 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 ให้สอดคล้องกัน นั่นคือ ซ่อนปุ่มลงชื่อเข้าใช้ เปิดกิจกรรมหลักของคุณ หรืออะไรก็ตามที่เหมาะสมสําหรับแอป
หาก GoogleSignIn.getLastSignedInAccount
ส่งคืน null
แสดงว่าผู้ใช้ยังไม่ได้ลงชื่อเข้าใช้แอปด้วย Google อัปเดต UI เพื่อแสดงปุ่ม Google Sign-In
เพิ่มปุ่ม Google Sign-In ในแอป
เพิ่ม
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 (เช่น ในเมธอด
onCreate
) ให้ลงทะเบียนOnClickListener
ของปุ่มเพื่อลงชื่อเข้าใช้เมื่อผู้ใช้คลิกเมื่อคลิกfindViewById(R.id.sign_in_button).setOnClickListener(this);
เริ่มขั้นตอนการลงชื่อเข้าใช้
ในส่วน
onClick
ของกิจกรรม ให้แตะปุ่มลงชื่อเข้าใช้โดยสร้าง Intent การลงชื่อเข้าใช้ด้วยเมธอดgetSignInIntent
และเริ่มต้น Intent ด้วยstartActivityForResult
@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); }
แบบเริ่มต้นจะแจ้งให้ผู้ใช้เลือกบัญชี Google ที่จะลงชื่อเข้าใช้ หากคุณขอขอบเขตเกิน
profile
,email
และopenid
ระบบจะขอให้ผู้ใช้มอบสิทธิ์เข้าถึงทรัพยากรที่ขอด้วยหลังจากที่ผู้ใช้ลงชื่อเข้าใช้แล้ว คุณจะเห็นออบเจ็กต์
GoogleSignInAccount
สําหรับผู้ใช้ในเมธอดonActivityResult
ของกิจกรรม@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
, รหัส Google ของผู้ใช้ (สําหรับการใช้งานฝั่งไคลเอ็นต์) กับgetId
และโทเค็นรหัสสําหรับผู้ใช้ที่มีgetIdToken
ได้ด้วย หากต้องการส่งผู้ใช้ที่ลงชื่อเข้าใช้ในปัจจุบันไปยังเซิร์ฟเวอร์แบ็กเอนด์ ให้ส่งโทเค็นรหัสไปยังเซิร์ฟเวอร์แบ็กเอนด์และตรวจสอบโทเค็นในเซิร์ฟเวอร์