การผสานรวม 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 หากต้องการขอที่อยู่อีเมลของผู้ใช้ด้วย ให้สร้างออบเจ็กต์ 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. จากนั้น ให้สร้างเมธอด 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 ในแอป

  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);
    }

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