หากต้องการผสานรวมการลงชื่อเข้าใช้ด้วย Google เข้ากับแอป Android ให้กำหนดค่าการลงชื่อเข้าใช้ด้วย Google และ เพิ่มปุ่มลงในเลย์เอาต์ของแอปที่จะเริ่มขั้นตอนการลงชื่อเข้าใช้
ก่อนเริ่มต้น
กำหนดค่าโปรเจ็กต์คอนโซล Google Cloud และตั้งค่าโปรเจ็กต์ Android Studio
กำหนดค่า Google Sign-In และออบเจ็กต์ GoogleSignInClient
- ใน - onCreateเมธอดของกิจกรรมการลงชื่อเข้าใช้ ให้กำหนดค่าการลงชื่อเข้าใช้ด้วย Google เพื่อ ขอข้อมูลผู้ใช้ที่แอปของคุณต้องการ ตัวอย่างเช่น หากต้องการกำหนดค่าการลงชื่อเข้าใช้ด้วย Google เพื่อขอรหัสและข้อมูลโปรไฟล์พื้นฐานของผู้ใช้ ให้สร้างออบเจ็กต์- 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เมื่อลงชื่อเข้าใช้ ให้ขอเฉพาะขอบเขตที่จำเป็นเพื่อให้แอปทำงานได้ขั้นต่ำ เพื่อให้ผู้ใช้ได้รับประสบการณ์ที่ดีที่สุด ขอขอบเขตเพิ่มเติมเฉพาะเมื่อคุณต้องการเท่านั้น เพื่อให้ผู้ใช้เห็นหน้าจอขอความยินยอมในบริบทของการดำเนินการที่ผู้ใช้ทำ ดูการขอขอบเขตเพิ่มเติม
- จากนั้นในเมธอด - onCreateของกิจกรรมการลงชื่อเข้าใช้ ให้สร้างออบเจ็กต์- GoogleSignInClientด้วยตัวเลือกที่คุณระบุ- // 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
เพิ่มปุ่มลงชื่อเข้าใช้ด้วย 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 (เช่น ในเมธอด - 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); }- การเริ่ม Intent จะแจ้งให้ผู้ใช้เลือกบัญชี 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ได้ด้วย หากต้องการส่งผู้ใช้ที่ลงชื่อเข้าใช้ไปยังเซิร์ฟเวอร์แบ็กเอนด์ ให้ส่งโทเค็นรหัสไปยังเซิร์ฟเวอร์แบ็กเอนด์และตรวจสอบ โทเค็นในเซิร์ฟเวอร์
