การผสานรวม Google Sign-In เข้ากับแอป Android ของคุณ

หากต้องการผสานรวม Google Sign-In กับแอป Android ให้กำหนดค่า Google Sign-In และ เพิ่มปุ่มลงในเลย์เอาต์ของแอปที่จะเริ่มขั้นตอนการลงชื่อเข้าใช้

ก่อนเริ่มต้น

กำหนดค่าโปรเจ็กต์คอนโซล Google API และตั้งค่าโปรเจ็กต์ Android Studio

กำหนดค่า Google Sign-In และออบเจ็กต์ GoogleSignInClient

  1. ในเมธอด onCreate ของกิจกรรมการลงชื่อเข้าใช้ ให้กำหนดค่า Google Sign-In เป็น ขอข้อมูลผู้ใช้ที่จำเป็นสำหรับแอปของคุณ เช่น หากต้องการกำหนดค่า Google Sign-In เพื่อขอรับผู้ใช้ และข้อมูลโปรไฟล์พื้นฐาน GoogleSignInOptions ที่มีพารามิเตอร์ DEFAULT_SIGN_IN วิธีส่งคำขอ email [อีเมล] ให้สร้างออบเจ็กต์ 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 เพื่อให้ผู้ใช้ได้รับประสบการณ์ที่ดีที่สุด เมื่อลงชื่อเข้าใช้ ให้ขอเฉพาะขอบเขตที่ ที่จำเป็นสำหรับแอปเพื่อให้ทำงานน้อยที่สุด ขอขอบเขตเพิ่มเติม ในเวลาที่คุณต้องการเท่านั้น เพื่อให้ผู้ใช้เห็นหน้าจอคำยินยอมใน บริบทของการดำเนินการที่ทำ โปรดดูหัวข้อการขอขอบเขตเพิ่มเติม

  2. จากนั้นในเมธอด 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 Sign-In

เพิ่มปุ่ม Google Sign-In ลงในแอป

  1. ปุ่มลงชื่อเข้าใช้ Google มาตรฐาน เพิ่ม SignInButton ในเลย์เอาต์ของแอปพลิเคชัน

    <com.google.android.gms.common.SignInButton
     android:id="@+id/sign_in_button"
     android:layout_width="wrap_content"
     android:layout_height="wrap_content" />
    
  2. ไม่บังคับ: หากคุณใช้กราฟิกปุ่มลงชื่อเข้าใช้เริ่มต้นแทน ที่ให้เนื้อหาปุ่มลงชื่อเข้าใช้ของคุณเอง คุณสามารถปรับแต่ง ที่มีขนาดด้วย setSize

    // Set the dimensions of the sign-in button.
    SignInButton signInButton = findViewById(R.id.sign_in_button);
    signInButton.setSize(SignInButton.SIZE_STANDARD);
    
  3. ในกิจกรรม Android (เช่น ในเมธอด onCreate) ให้ลงทะเบียน OnClickListener ของปุ่มของคุณเพื่อลงชื่อเข้าใช้ให้ผู้ใช้งานเมื่อคลิก:

    findViewById(R.id.sign_in_button).setOnClickListener(this);
    

เริ่มขั้นตอนการลงชื่อเข้าใช้

  1. รูปภาพตัวเลือกบัญชีผู้ใช้สำหรับลงชื่อเข้าใช้ ในเมธอด 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 ผู้ใช้จะได้รับแจ้งให้ให้สิทธิ์เข้าถึงทรัพยากรที่ขอด้วย

  2. หลังจากผู้ใช้ลงชื่อเข้าใช้ คุณจะได้รับ 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 หากคุณจำเป็นต้องส่งผู้ใช้ที่ลงชื่อเข้าใช้อยู่ไปยังเซิร์ฟเวอร์แบ็กเอนด์ ส่งโทเค็นรหัสไปยังเซิร์ฟเวอร์แบ็กเอนด์ และตรวจสอบโทเค็นในเซิร์ฟเวอร์