การผสานรวม 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);
    }

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